GetType and Interfaces

  • Thread starter Thread starter Grande
  • Start date Start date
G

Grande

Hi all,

I'm trying to use GetType to get an Interface, but it's not working?
Is there a special way you have to do this, since it's an Interface
(rather than a complete class)?

Code:
Type SpecDataType = SpecData.GetType();
string strTemp = "SCG.Persistence.Interfaces.Product.I" +
SpecDataType.Name + "Dao";
//string strTemp = "I" + SpecDataType.Name + "Dao";
Type SpecDataInterfaceType = Type.GetType(strTemp);

Thanks!
 
Grande said:
I'm trying to use GetType to get an Interface, but it's not working?
Is there a special way you have to do this, since it's an Interface
(rather than a complete class)?

Code:
Type SpecDataType = SpecData.GetType();
string strTemp = "SCG.Persistence.Interfaces.Product.I" +
SpecDataType.Name + "Dao";
//string strTemp = "I" + SpecDataType.Name + "Dao";
Type SpecDataInterfaceType = Type.GetType(strTemp);

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that Type.GetType needs the full assembly name (version
information etc) unless you're getting a type from the calling assembly
or mscorlib.
 
Hello Grande,

The Type.GetType method requires you to type the Assembly fully qualified
name of the Type, which means you have to include the assembly name, version,
culture and public key token (if any). For instance,
"System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" to obtain the Type.Int32 instance from .NET
Framework v2.0.
 
Stanimir Stoyanov said:
The Type.GetType method requires you to type the Assembly fully qualified
name of the Type, which means you have to include the assembly name, version,
culture and public key token (if any). For instance,
"System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" to obtain the Type.Int32 instance from .NET
Framework v2.0.

Note that that's only true if the type isn't in the calling assembly or
mscorlib. For example, Type.GetType ("System.Int32") will work fine for
your example, as Int32 is in mscorlib.
 
Thank you for pointing this out, Jon.
--
Best regards,

Stanimir Stoyanov
(e-mail address removed)
 
Grande said:
Hi all,

I'm trying to use GetType to get an Interface, but it's not working?
Is there a special way you have to do this, since it's an Interface
(rather than a complete class)?

Code:
Type SpecDataType = SpecData.GetType();
string strTemp = "SCG.Persistence.Interfaces.Product.I" +
SpecDataType.Name + "Dao";
//string strTemp = "I" + SpecDataType.Name + "Dao";
Type SpecDataInterfaceType = Type.GetType(strTemp);

Thanks!
Not exactly sure what you want but the following works for me.
It gets the interface 2 ways. The first way is if it is part of the
loaded assembly. The second is if you used reflection to load an
assembly like Assembly.LoadFrom and want to get the interface from it.
Note in both cases you have to use the fully qualified name including
the namespace.

namespace TestNamespace
{
interface TestInterface
{
void DoSomething();
}
class TestClass
{
[STAThread]
static void Main(string[] args)
{
Type ts = Type.GetType("TestNamespace.TestInterface");
Assembly asem = ts.Assembly;
Type ts2 = asem.GetType("TestNamespace.TestInterface");
}
}
}
 
Back
Top