By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Check Interface Implementation of a Type

P: n/a
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.

Apr 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
So the issue I'm having is whether I can check a type implements an
interface.


Sure, check out the Type.IsAssignableFrom method.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 1 '06 #2

P: n/a
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?

Apr 1 '06 #3

P: n/a

Hayato Iriumi wrote:
<snip>
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... <snip> Console.WriteLine(cust.GetType().IsAssignableFrom( GetType(ITest))) <snip> 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

Apr 1 '06 #4

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

Apr 2 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.