How to check that type is inherited from some interface c#
Asked Answered
K

6

33

I have following:

Assembly asm = Assembly.GetAssembly(this.GetType());

foreach (Type type in asm.GetTypes())
{
    MyAttribute attr = Attribute.GetCustomAttribute(type, typeof(MyAttribute))    as MyAttribute;
     if(attr != null && [type is inherited from Iinterface])
     {
        ...
     }

}

How can i check that type is inherited from MyInterface? Does is keywork will work in this way?

Thank you.

Kramatorsk answered 2/12, 2010 at 11:57 Comment(0)
M
60

No, is only works for checking the type of an object, not for a given Type. You want Type.IsAssignableFrom:

if (attr != null && typeof(IInterface).IsAssignableFrom(type))

Note the order here. I find that I almost always use typeof(...) as the target of the call. Basically for it to return true, the target has to be the "parent" type and the argument has to be the "child" type.

Mignonne answered 2/12, 2010 at 11:59 Comment(0)
T
7

Check out IsAssignableFrom http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx

Telegraphic answered 2/12, 2010 at 11:59 Comment(0)
G
3

Hi You can use type.GetInterfaces() or type.GetInterface() to get the interfaces which the type implements.

Galley answered 2/12, 2010 at 12:0 Comment(0)
A
-1

Given worst case scenario;

if you are using reflection over all properties in a class...

public List<PropertyInfo> FindProperties(Type TargetType) {

     MemberInfo[] _FoundProperties = TargetType.FindMembers(MemberTypes.Property,        
     BindingFlags.Instance | BindingFlags.Public, new
     MemberFilter(MemberFilterReturnTrue), TargetType);

     List<PropertyInfo> _MatchingProperties = new List<PropertyInfo>();

     foreach (MemberInfo _FoundMember in _FoundProperties)  {
     _MatchingProperties.Add((PropertyInfo)_FoundMember); }

     return _MatchingProperties;

}

IInterface is some generic interface

   public void doSomthingToAllPropertiesInDerivedClassThatImplementIInterface() {

        IList<PropertyInfo> _Properties = FindProperties(this.GetType());
        foreach (PropertyInfo _Property in _Properties)
        {

            if (_Property.PropertyType.GetInterfaces().Contains(typeof(IInterface)))
            {
                if ((IInterface)_Property.GetValue(this, null) != null)
                {
                      ((IInterface)_Property.GetValue(this, null)).SomeIInterfaceMethod();  
                }
            }
        }
    }
Abnormal answered 11/7, 2013 at 18:28 Comment(0)
T
-1

You can use type.IsAssignableTo(typeof(IInterface)), as it returns true in one of the following cases, as described in the MSDN:

  • The current instance and targetType represent the same type.
  • The current type is derived either directly or indirectly from targetType. The current type is derived directly from targetType if it inherits from targetType; the current type is derived indirectly from targetType if it inherits from a succession of one or more classes that inherit from targetType.
  • targetType is an interface that the current type implements.
  • The current type is a generic type parameter, and targetType represents one of the constraints of the current type.
Trews answered 27/6, 2024 at 17:54 Comment(0)
R
-2

Realise this is very late, but leaving it here for reference: I found the is operator does the job - from MSDN - http://msdn.microsoft.com/en-us/library/scekt9xw(v=vs.71).aspx

Using resharper on Jon Skeets answer, gave me "is" as a suggestion as well.

Readus answered 29/10, 2012 at 12:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.