beaucrawford.net

Give me data or give me death

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Don't show

Authors

Tags

Don't show

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010

    Using Reflection to get inherited properties for an interface

    This will probably throw you for a loop the first time you encounter it.  If you have an interface that derives from another interface and you want to get all of the properties (including those from the parent interface) then you cannot simply call GetProperties (like you can do with classes).

    Example:

    public interface IPerson
    {
        string FirstName
        {
            get;
        }
    
        string LastName
        {
            get;
        }
    }
    
    public interface IManager : IPerson
    {
        int ManagerID
        {
            get;
        }
    
        int DepartmentID
        {
            get;
        }
    }

    If you call typeof(IManager).GetProperties() you will only have access to the properties that are directly part of IManager, i.e. ManagerID and DepartmentID.  If you need all properties, including those from IPerson, then you must use the GetInterfaces method from Type.  Here’s a helper method that does just that:

    public static PropertyInfo[] GetAllProperties(Type type)
    {
        List<Type> typeList = new List<Type>();
        typeList.Add(type);
    
        if (type.IsInterface)
        {
            typeList.AddRange(type.GetInterfaces());
        }
    
        List<PropertyInfo> propertyList = new List<PropertyInfo>();
    
        foreach (Type interfaceType in typeList)
        {
            foreach (PropertyInfo property in interfaceType.GetProperties())
            {
                propertyList.Add(property);
            }
        }
    
        return propertyList.ToArray();
    }

    You can now simply call GetAllProperties(typeof(IManager)) to have all properties returned, including those from IPerson.


    Categories: C# | Reflection
    Posted by Beau on Sunday, September 21, 2008 1:29 PM
    Permalink | Comments (5) | Post RSSRSS comment feed

    Comments

    Joshua Hanson us

    Wednesday, February 18, 2009 11:57 AM

    This is really great. I didn't know this about interfaces until now. One recommendation for being thorough, when you call interfaceType.GetProperties(), you might want to make a recursive call to your method in case that interface inherits from deeper interfaces.

    The Geek gb

    Tuesday, May 19, 2009 6:33 AM

    Bit of a flaw in .net IMHO - but well done for posting Wink

    Joshua - .GetInterfaces() is recursive - it returns ALL inherited interfaces so there is no need to write a recursive function.

    Charles Sustek us

    Tuesday, May 19, 2009 9:16 AM

    Great article! I've taken your code and written it as an extension method. Behold:

    <pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>namespace System
    {
    public static class TypeExtensionMethods
    {
    public static PropertyInfo[] GetAllProperties(this Type type)
    {
    List&lt;Type&gt; typeList = new List&lt;Type&gt;();
    typeList.Add(type);

    if (type.IsInterface)
    {
    typeList.AddRange(type.GetInterfaces());
    }

    List&lt;PropertyInfo&gt; propertyList = new List&lt;PropertyInfo&gt;();

    foreach (Type interfaceType in typeList)
    {
    foreach (PropertyInfo property in interfaceType.GetProperties())
    {
    propertyList.Add(property);
    }
    }

    return propertyList.ToArray();
    }
    }
    }
    </code></pre>

    Charles Sustek us

    Tuesday, May 19, 2009 9:17 AM

    Sorry! Here it is in a better format.

    namespace System
    {
    public static class TypeExtensionMethods
    {
    public static PropertyInfo[] GetAllProperties(this Type type)
    {
    List<Type> typeList = new List<Type>();
    typeList.Add(type);

    if (type.IsInterface)
    {
    typeList.AddRange(type.GetInterfaces());
    }

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

    foreach (Type interfaceType in typeList)
    {
    foreach (PropertyInfo property in interfaceType.GetProperties())
    {
    propertyList.Add(property);
    }
    }

    return propertyList.ToArray();
    }
    }
    }

    Charles Sustek us

    Tuesday, May 19, 2009 9:17 AM

    OK I tried. I've placed it on my blog.