Check Interface Implementation of a Type

H

Hayato Iriumi

Hello folks,

I'm dynamically loading assembly by looping through types in the
assembly. But I want to load the types that implements a interface. So
the issue I'm having is whether I can check a type implements an
interface.

Of course, I could load the type by invoking CreateInstance of the
assembly and do TryCast, but I want to know whether the type implements
the interface before I load the type.
 
H

Hayato Iriumi

Hello,
Hello, thank you very much for your reply.

I tried IsAssignableFrom myself, but it always returns false. I have an
Interface "ITest". And my "Customer" class implements "ITest". Here is
what I did...

Sub Main()
Dim cust As New Customer(1, "MyFirstName", "MyLastName")

Console.WriteLine(cust.GetType().IsAssignableFrom(GetType(ITest)))
Console.Read()
End Sub

I would expect IsAssignableFrom() function to return True, but it
return false. Am I doing something wrong?
 
B

Branco Medeiros

Hayato Iriumi wrote:
I tried IsAssignableFrom myself, but it always returns false. I have an
Interface "ITest". And my "Customer" class implements "ITest". Here is
what I did...
Console.WriteLine(cust.GetType().IsAssignableFrom(GetType(ITest)))
I would expect IsAssignableFrom() function to return True, but it
return false. Am I doing something wrong?

According to the docs, it seems you should be dowing:

Console.WriteLine(GetType(ITest).IsAssignableFrom(cust.GetType()))

because A.IsAssignableFrom(B) will be true if B is a type that
implements A (and not the other way around)...

Regards,

Branco
 
H

Hayato Iriumi

Yes, you're right about that. I tested it works now. Thank you very
much for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top