The issue appears is when I have a class implementing an interface, and extending a class which implements an interface:
class Some : SomeBase, ISome {}
class SomeBase : ISomeBase {}
interface ISome{}
interface ISomeBase{}
Since typeof(Some).GetInterfaces() returns and array with ISome and ISomeBase, i'm not able to distinguish if ISome is implemented or inherited (as ISomeBase). As MSDN I can't assume the order of the interfaces in the array, hence I'm lost. The method typeof(Some).GetInterfaceMap() does not distinguish them either.
Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class.
On implementation of an interface, you must override all of its methods. Interfaces can contain properties and methods, but not fields/variables. Interface members are by default abstract and public. An interface cannot contain a constructor (as it cannot be used to create objects)
Some of the interface types in C# include. IEnumerable − Base interface for all generic collections. IList − A generic interface implemented by the arrays and the list type. IDictionary − A dictionary collection.
You just need to exclude the interfaces implemented by the base type :
public static class TypeExtensions
{
public static IEnumerable<Type> GetInterfaces(this Type type, bool includeInherited)
{
if (includeInherited || type.BaseType == null)
return type.GetInterfaces();
else
return type.GetInterfaces().Except(type.BaseType.GetInterfaces());
}
}
...
foreach(Type ifc in typeof(Some).GetInterfaces(false))
{
Console.WriteLine(ifc);
}
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