Reputation: 373
I am attempting to retrieve the child classes of an object while omitting primitive types.
public class Dog
{
public int Id {get;set;}
public int Name {get;set;}
public Breed Breed {get;set;}
}
var dog = new Dog(); var children = dog.GetType().GetProperties(BindingFlags.Instance);
Why does the children array not contain the breed property?
Upvotes: 0
Views: 777
Reputation: 7187
By supplying only BindingFlags.Instance
, you can't get any properties at all, because you are not sending any access modifier predicate.
According to your needs, combine these flags with bitwise OR operator |
You can find the documentation here: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.bindingflags?view=netframework-4.8
var children = dog.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
EDIT:
Unfortunately, the enumeration does not have any value for filtering the properties according to their value types. To make this a complete answer, the filtering to an array containing only the Breed
property is as contributed by @const-phi:
var result = children.Where(c => c.PropertyType.IsClass).ToArray(); // Const Phi
Upvotes: 2