0

I want to use GetProperties to get the properties from the parent class via the child and, despite researching this, have been unsuccessful.

I tried the next without any result:

PropertyInfo[] fields = t.GetProperties();
PropertyInfo[] fields1 = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
PropertyInfo[] propNames = t.BaseType.GetProperties( BindingFlags.Public | BindingFlags.Instance);

Just got it the properties from the child class, but dont get the properties from the parent.

Classes

public class A: B
{
    public string a1 { get; set; }

    public string a2 { get; set; }

    public string a3 { get; set; }

    public string a4 { get; set; }
}

public class B
{
    public string b1;
}

Using this code I am getting A's properties but not the property in B.

Does this code work? Do I need to configure something in some place?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60

1 Answers1

2

In your declaration

public class B
{
    public string b1;
}

b1 is a field, not a property. You should either

  • Use GetFields():

    FieldInfo[] fields = t.GetFields();
    

    which will get the fields (as expected) - note that the documentation says that

    Generally, you should use fields only for variables that have private or protected accessibility.

  • Make b1 a property, e.g. by adding { get; set; } accessors to it.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92