Immediate Interfaces for a type

If anybody is interested to get the immediate interfaces for a type, this a way to get it:

 

          /// <summary>
/// Gets the immediate interfaces.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public static Type[] GetImmediateInterfaces(Type type)
{
Type[] interfaces = type.GetInterfaces();
return GetImmediateInterfaces(interfaces);
}

/// <summary>
/// Gets the immediate interfaces.
/// </summary>
/// <param name="interfaces">The interfaces.</param>
/// <returns></returns>
public static Type[] GetImmediateInterfaces(Type[] interfaces)
{
List<Type> immediateInterfaces = new List<Type>();

foreach (Type possibleImmediateInterface in interfaces)
{
bool isBaseInterface = false;

foreach (Type baseInterface in interfaces)
{
if (possibleImmediateInterface != baseInterface && possibleImmediateInterface.IsAssignableFrom(baseInterface))
{
isBaseInterface = true;
break;
}
}

if (!isBaseInterface)
immediateInterfaces.Add(possibleImmediateInterface);
}

return immediateInterfaces.ToArray();
}

No Comments