locked
How to get properties of base class using reflection RRS feed

  • Question

  • Hello,

    Is there any way to get all the properties of the class including the properties which is in its base class.

    Currently I am using this code but it only gives me properties of the particular class only.

       TypeInfo info = typeof(DerivedClass).GetTypeInfo();
    
            IEnumerable<PropertyInfo> pList = info.DeclaredProperties;
    
        foreach (var valueCheck in pList)
         {
               if (valueCheck.GetValue(objectofderivedclass, null) == null)
                 valueCheck.SetValue(objectofderivedclass, "");
          }

    Thanks in advance.

      



    • Edited by zee_patel Friday, April 4, 2014 1:06 PM
    Friday, April 4, 2014 5:15 AM

Answers

  • Here's some code I wrote to do this:

    IEnumerable<PropertyInfo> GetAllProperties(Type T)
    {
      IEnumerable<PropertyInfo> PropertyList = T.GetTypeInfo().DeclaredProperties;
      if (T.GetTypeInfo().BaseType != null)
      {
        PropertyList = PropertyList.Concat(GetAllProperties(T.GetTypeInfo().BaseType));
      }
      return PropertyList;
    }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by zee_patel Monday, April 7, 2014 5:53 AM
    Friday, April 4, 2014 4:02 PM
    Moderator

All replies

  • My first thought is: why don't you just use recursion?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, April 4, 2014 12:07 PM
    Moderator
  • Hi Matt,

    Thanks for the reply, It will be helpful if you can post some code here.

    I tried the code as mention here, but As there is no GetMembers() method I find it difficult to iterate to the base class.



    • Edited by zee_patel Friday, April 4, 2014 1:13 PM
    Friday, April 4, 2014 1:12 PM
  • Here's some code I wrote to do this:

    IEnumerable<PropertyInfo> GetAllProperties(Type T)
    {
      IEnumerable<PropertyInfo> PropertyList = T.GetTypeInfo().DeclaredProperties;
      if (T.GetTypeInfo().BaseType != null)
      {
        PropertyList = PropertyList.Concat(GetAllProperties(T.GetTypeInfo().BaseType));
      }
      return PropertyList;
    }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by zee_patel Monday, April 7, 2014 5:53 AM
    Friday, April 4, 2014 4:02 PM
    Moderator