Interface i1
End Interface
Public Class c1
Implements i1
End Class
Public Class c2
Inherits c1
End Class
How to determine (using reflection) that c2 have not implemented i1 ?
Interface i1
End Interface
Public Class c1
Implements i1
End Class
Public Class c2
Inherits c1
End Class
How to determine (using reflection) that c2 have not implemented i1 ?
I'll say that in the most generic case, while it is a metainformation that is surely contained in the assembly, it can't be discovered with .NET reflection.
public class DerivedWithInterface : BaseClass, IInterface
{
}
public class DerivedWithoutInterface : BaseClass
{
}
public interface IInterface
{
void Method();
}
public class BaseClass : IInterface
{
public void Method()
{
Console.WriteLine("C1");
}
}
I've found no way to distinguish between DerivedWithInterface
and DerivedWithoutInterface
, BUT the IL code is different.
To ask the main question, you can check whether a Class has an implemented interface in this way:
Dim hasInterface As Boolean = GetType(type).GetInterfaces.Contains(GetType(MyInterface))
A reflection example:
Dim ass As Assembly = Assembly.GetExecutingAssembly
Dim types As Type() = ass.GetTypes()
For Each t As Type In types
If t.GetInterfaces.Contains(GetType(MyInterface)) Then
Console.WrtieLine(String.Format("Found: {0}, t.Name))
End If
Next t
In your case, you don't have to use reflection. C2 does implement your interface i1, because it inherits from C1 and therefore inherits of all the methods and properties that C1 provides. So if C1 provides an implementation for i1, so does C2. That is the magic of Object Oriented Programming...
Public Interface I1
Function Method1() As String
End Interface
Public Class C1
Implements I1
Public Function Method1() As String Implements I1.Method1
Return "Hello"
End Function
End Class
Public Class C2
Inherits C1
End Class
'Somewhere in your code
Dim Test As New C2()
Test.Method1() 'It does work, so C2 Implements I1 because it inherits from C1
You can also say, if you want to Reimplement the interface :
Public Class C2
Inherits C1
Implements I1
'You have to declare it Overload because this function already exists
Public Overloads Function Method1() as String Implements I1.Method1
Return "Hello From C2"
End Function
End Class
But what it will do is that it will replace the first implementation by the one defined in C1. And since it is a replacement there is no way to know it...
Dim C As New C2()
Dim T = C.GetType()
For Each I In T.GetInterfaces()
Dim map = T.GetInterfaceMap(I)
Dim target = map.TargetType
MessageBox.Show(target.Name) 'Will always display C2, whether or not you reimplement it.
Next
Since its not very clear what information exactly you are looking for, here are two approches:
First, you can use the Type.GetInterfaceMap Method to check if the type implements a method of the interface:
public static bool TypeImplementsInterfaceMethod(this Type t, Type interfaceType)
{
if (t.IsInterface) return false;
if (!t.GetInterfaces().Contains(interfaceType)) return false;
return t.GetInterfaceMap(interfaceType).TargetMethods.Any(method => method.DeclaringType == t);
}
Second, you can check if another interface or the basetype already implements the interface:
public static bool TypeDirectlyImplementsInterface(this Type t, Type interfaceType)
{
var interfaces = t.GetInterfaces();
if (!interfaces.Contains(interfaceType)) return false;
if (interfaces.Where(type => type != interfaceType)
.SelectMany(type => type.GetInterfaces())
.Contains(interfaceType))
return false;
if (t.BaseType == null) return true;
return !t.BaseType.GetInterfaces().Contains(interfaceType);
}