I need to know if the Type interface implements the interface.
Dim asmRule As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Rule.dll")) For Each typeAsm As System.Type In asmRule.GetTypes If TypeOf typeAsm Is Rule.IRule Then 'that does always return false even though they implement IRule' End If Next
Thanks to everyone. Now I know why typeof did not work. The type, of course, does not implement IRule. I have filtered two options from your answers:
GetType(Rule.IRule).IsAssignableFrom(typeAsm)typeAsm.GetInterface(GetType(Rule.IRule).FullName) IsNot Nothing
What is the best performance choice?
UPDATE : I found out that it is better to use:
Not typeAsm.GetInterface(GetType(Rule.IRule).FullName) Is Nothing
instead
GetType(Rule.IRule).IsAssignableFrom(typeAsm)
because the IRule interface itself can be assigned to IRule, which calls MissingMethodExcpetion if I try to instantiate:
ruleObj = CType(Activator.CreateInstance(typeAsm, True), Rule.IRule)
UPDATE2: Thanks to Ben Voigt. He convinced me that IsAssignableFrom in combination with IsAbstract might be the best way to check if a given type of interface implements the interface and not the interface itself (which raises a MissingMethodException if you are trying to instantiate).
If GetType(Rule.IRule).IsAssignableFrom(typeAsm) AndAlso Not typeAsm.IsAbstract Then
Tim schmelter
source share