I have a type and an interface and I need to verify that the type implements the interface abstractly.
I have set to write a brute force code using Reflection and it is pretty ugly.
I am wondering if there is a better way than the brute force implementation I am doing now.
Any ideas?
Thanks.
EDIT
Have not checked the implementation yet, but the brute force draft code looks like this:
public static bool IsAbstractInterfaceImplementation(Type someType, Type someInterface)
{
if (!someInterface.IsAssignableFrom(someType))
{
return false;
}
if (!someType.IsAbstract)
{
return false;
}
var m_interfaceMemberNames = someInterface.GetMembers().Select(m => m.Name).ToList();
// Make sure every interface member implementation is abstract.
foreach (var typeMember in someType.FindMembers(MemberTypes.Event | MemberTypes.Property | MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance, null, null))
{
if (m_interfaceMemberNames.Contains(typeMember.Name))
{
MethodInfo method;
// Make sure the ancestor member is abstract.
switch (typeMember.MemberType)
{
case MemberTypes.Event:
if (!IsAbstractImplementation(((EventInfo)typeMember).GetAddMethod()))
{
return false;
}
method = ((EventInfo)typeMember).GetRemoveMethod();
break;
case MemberTypes.Property:
method = ((PropertyInfo)typeMember).GetGetMethod();
default:
method = (MethodInfo)typeMember;
break;
}
if (!IsAbstractImplementation(method))
{
return false;
}
}
}
return true;
}
public static bool IsAbstractImplementation(MethodInfo methodInfo)
{
const MethodAttributes expectedAttributes =
MethodAttributes.Abstract |
MethodAttributes.Public |
MethodAttributes.NewSlot |
MethodAttributes.Virtual;
return (methodInfo.Attributes & expectedAttributes) == expectedAttributes;
}
Without compiling it I already see a problem with properties, that the code has to check whether interface defines getter and/or setter and verify the right method(s), instead of blindly assuming the getter. Anyway, as one can see, the code is pretty dull. I am wondering if there is a better way...
EDIT 2
You can determine if a type implements a particular interface by using Type.IsAssignableFrom:
typeof(MyInterface).IsAssignableFrom(abstractType);
Edit: after clarification was added to the answer - to determine if all of an interface's implementations are abstract for a given class, you can do so much more easily by getting an InterfaceMap for the type in question:
bool IsAbstractOfInterface(Type classType, Type interfaceType)
{
var map = classType.GetInterfaceMap(interfaceType);
foreach (var info in map.TargetMethods)
{
if (!info.IsAbstract)
{
return false;
}
}
return true;
}
Or maybe a generic extension method...
public static bool IsAbstractOf<TInterface>(this Type type)
{
var map = type.GetInterfaceMap(typeof(TInterface));
foreach (var info in map.TargetMethods)
{
if (!info.IsAbstract)
{
return false;
}
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With