Question

I have got three classes as follows:

public class TestA
{
    public string Str1 { get; set; }
    public string Str2 { get; set; }
    public List<TestB> LstTestBs { get; set; }
    public TestC ObjTestC { get; set; }
}

public class TestB
{
    public string Str3 { get; set; }
    public string Str4 { get; set; }
}

public class TestC
{
    public string Str5 { get; set; }
}

I have tried:

var prop = typeof (TestA).GetProperties();

But, it is giving only the PropertyInfo for the four members inside TestA. I need to get the PropertyInfo for all the members in the TestA, TestB and TestC classes.

Please help... Thanks in advance, San

Était-ce utile?

La solution 2

Thanks for the help everyone. I have got the answer.

        var prop = typeof (TestA).GetProperties();

        for (int i=0;i<prop.Count();i++)
        {
            var propertyInfo = prop[i];
            if (propertyInfo.PropertyType.Namespace != "System")
            {
                if (propertyInfo.PropertyType.IsGenericType &&
                    propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof (List<>))
                {
                    Type itemType = propertyInfo.PropertyType.GetGenericArguments()[0]; 
                    var listObjectProperties = itemType.GetProperties();
                    prop = prop.Union(listObjectProperties).ToArray();

                }
                else
                {

                    var childProp = propertyInfo.PropertyType.GetProperties();
                    prop = prop.Union(childProp).ToArray();
                }
            }
        }

Autres conseils

If you put all your classes in the same namespace, you can collect the properties by enumerating the classes in the namespace, instead of mining the property structure:

Getting all types in a namespace via reflection

SLaks is right. You should do this recursively. See wikipedia's article on Recursion for more information on the concept. For example, in your case, this is the general idea:

public void AddPropertiesAndChildPropertiesToList(Type type, List<PropertyInfo> list)
{
    var properties = type.GetProperties();
    list.AddRange(properties);
    foreach (var property in properties)
    {
        // recursive methods are ones that call themselves, like this...
        AddPropertiesAndChildPropertiesToList(property.PropertyType, list);
    }
}

Note that this example is lacking several things:

  • Most importantly, it has no guard against infinite recursion. You could fix this by keeping track of where you'd already been with a Stack<Type> alreadyVisited parameter. If you find you've been asked to add the list of properties for a type you've already visited, just return out of the method instead, or throw an exception.
  • As I mentioned in your other related question, for your purposes you really need to be keeping track of property chains, not just properties. The alreadyVisited stack would be useful here, too.
  • It won't handle your List<TestB> in any useful way. For that, you probably need to figure out whether the type has an indexer, and then the properties of the type that is returned by that indexer.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top