0

i have this model :

public class model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }
    public int RoleId { get; set; }
    public Role Role { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }
    public string SEC { get; set; }
}

i need search in the model and find propertys of type is Class but it can not find them .

i try by this code :

PropertyInfo[] props = model.GetType().GetProperties();
foreach (var item in props)
{
    if (item.PropertyType.IsClass)
    {
        PropertyInfo[] subClassProp = item.GetType().GetProperties();
        foreach (var itemB in subClassProp)
        {
            selectList.Add(itemB.Name);
        }
     }
}

How Can i Solve This Problem ?

vahdet
  • 6,357
  • 9
  • 51
  • 106
  • What is the inner loop for? – user2864740 Jan 03 '20 at 06:37
  • 1
    Anyway, item.PropertyType.GetProperties() should be used as it is above: item.GetType() returns a Type of PropertyInfo representing the meta-information, not the type of the property itself. This can be made more clear by using a variable: var propertyType = item.PropertyType; // now use “propertyType” consistently – user2864740 Jan 03 '20 at 06:38
  • Do you intend to get the property names of mode.Role property ? – Anu Viswan Jan 03 '20 at 06:41
  • You're calling `item.GetType().GetProperties()`. Item is a PropertyInfo, so you are pulling out all the properties of the PropertyInfo class. When doing reflection, stepping through your code with the debugger and inspecting all the variables is really required. It's easy to mix up the incantations – Flydog57 Jan 03 '20 at 06:43
  • @AnuViswan i need to find type of property . if it type is class it must search in that propertys . – kianoush dortaj Jan 03 '20 at 06:45
  • Does this answer your question? [How do I determine if a property is a user-defined type in C#?](https://stackoverflow.com/questions/23793845/how-do-i-determine-if-a-property-is-a-user-defined-type-in-c) – Jawad Jan 03 '20 at 06:47
  • @Flydog57 i using the `item.GetType().GetProperties()` but i dont know how can i find this property is class type ? – kianoush dortaj Jan 03 '20 at 06:47

1 Answers1

2

Believe you are attempting to do the following.Explanation is given below the code

PropertyInfo[] props = typeof(model).GetProperties(BindingFlags.Public|BindingFlags.Instance);
foreach (var item in props)
{
   if (item.PropertyType.IsClass && item.PropertyType.Assembly.FullName == typeof(model).Assembly.FullName)
    {
            PropertyInfo[] subClassProp = item.PropertyType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
            foreach (var itemB in subClassProp)
            {
                // Your code 
            }
    }
}

In the code given in OP, Following code was attempting to get Properties of PropertyInfo

item.GetType().GetProperties();

Instead you need to use

item.PropertyType.GetProperties();

Further, you can focus on only the public instance properties by using the Binding Flags

.GetProperties(BindingFlags.Public|BindingFlags.Instance);

Another case to be considered is that String would be considered as Class. For the same reason, if you intention is to capture only the User Defined Classes, you could use following condition

item.PropertyType.Assembly.FullName == typeof(model).Assembly.FullName
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51