System.Type

  • Thread starter Thread starter Semut
  • Start date Start date
S

Semut

Hello, below is a code snippet on determine what class does the class
inherit from. This is working fine until I need a requirement to determine
the on what interface does the class implement. Any idea anyone?

thanks in advance.




Type[] types = myAssembly.GetTypes();
foreach(Type t in types)
{


if (t.IsSubclassOf(typeof(InternetNow.IInowForm)))
{
....
}

}
 
Hello, below is a code snippet on determine what class does the class
inherit from. This is working fine until I need a requirement to determine
the on what interface does the class implement. Any idea anyone?

thanks in advance.

Type[] types = myAssembly.GetTypes();
foreach(Type t in types)
{

if (t.IsSubclassOf(typeof(InternetNow.IInowForm)))
{
...
}

}

if (t as InternetNow.IInowForm != null)
{
...
}

or

if(t.GetInterface("InternetNow.IInowForm") != null)
{
...
}
 
Semut said:
Hello, below is a code snippet on determine what class does the class
inherit from. This is working fine until I need a requirement to determine
the on what interface does the class implement. Any idea anyone?

System.Type.FindInterfaces
 
The first one not working but the second method does.

thanks

Tom Porterfield said:
Hello, below is a code snippet on determine what class does the class
inherit from. This is working fine until I need a requirement to
determine
the on what interface does the class implement. Any idea anyone?

thanks in advance.

Type[] types = myAssembly.GetTypes();
foreach(Type t in types)
{

if (t.IsSubclassOf(typeof(InternetNow.IInowForm)))
{
...
}

}

if (t as InternetNow.IInowForm != null)
{
...
}

or

if(t.GetInterface("InternetNow.IInowForm") != null)
{
...
}
 
Semut,
In addition to the other comments:

You might be able to use Type.IsAssignableFrom in both cases.

Something like:
if (typeof(InternetNow.IInowForm).IsAssignableFrom(t))


Hope this helps
Jay

Semut said:
Hello, below is a code snippet on determine what class does the class
inherit from. This is working fine until I need a requirement to determine
the on what interface does the class implement. Any idea anyone?

thanks in advance.




Type[] types = myAssembly.GetTypes();
foreach(Type t in types)
{


if (t.IsSubclassOf(typeof(InternetNow.IInowForm)))
{
...
}

}
 
Semut said:
A bit overkill for my purpose but it seems like a possible way.

Overkill indeed, and a 2nd look shows Type.GetInterfaces to be
probably the best way to go. More efficient, too, than parsing type
information from a string, as with GetInterface.
 
Back
Top