1

I need to list all properties for containing class, I have a lot of them, so I don't know the type in the code, but I can get it trough prop.BastType.FullName, but I can't cast it, and I don't want to change all my classes to crate "Cast" methods everywere. To avoid printing Property Properties such as DeclaringType,ReflectedType, etc, I'm excluding them in the code, but I don't like this way...

if (("DeclaringType,ReflectedType,MetadataToken,Module,PropertyType,Attributes,CanRead" +
                    ",CanWrite,GetMethod,SetMethod,IsSpecialName,CustomAttributes,MemberType")
                    .Contains(prop.Name))
                    continue;

full code of Method:

private static void GetAllPropertiesFor(object oo, int level, List<KeyValuePair<string,string>> result)
{
    Type entType = oo.GetType();
    IList<PropertyInfo> props = new List<PropertyInfo>(entType.GetProperties());
    foreach (PropertyInfo prop in props)
    {
        object propValue = prop.GetValue(oo, null);
        if (("DeclaringType,ReflectedType,MetadataToken,Module,PropertyType,Attributes,CanRead" +
            ",CanWrite,GetMethod,SetMethod,IsSpecialName,CustomAttributes,MemberType")
            .Contains(prop.Name))
            continue;
        if (propValue == null)
        {
            result.Add(new KeyValuePair<string, string>((new string(' ', level * 2)) + prop.Name, "Object"));
            //GetAllPropertiesFor(prop, level + (1*3), result);
        }
        else
        {
            if (result == null) result = new List<KeyValuePair<string, string>>();
            result.Add(new KeyValuePair<string, string>((new string(' ', level)) + prop.Name, propValue.ToString()));
        }
        if (entType.Namespace == "System.Data.Entity.DynamicProxies" && entType.BaseType.FullName.StartsWith("RE2012.Data.Models."))
        {
            GetAllPropertiesFor(prop, level + 1, result);
        }
        // Do something with propValue
    }
}

any suggestions?

this way I don't have in results, the values of:

PropertyType : System.Int32
Attributes : None
CanRead : True
CanWrite : True

just what I need.

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Alexei
  • 1,289
  • 3
  • 19
  • 34

1 Answers1

1

Disclaim i can't really work out what you want so this answer is just a shot in the dark

Based on what i think i understood you want to get the Properties from a specific Class so here is my example:

First my same Inheritance

class BaseClass
{
    public int Base { get; set; }
}
class ChildClass : BaseClass
{
    public double Child { get; set; }
}
class ChildChildClass : ChildClass
{
    public string ChildChild { get; set; }
}

now lets find the single Properties

class Program
{

    static void Main(string[] args)
    {
        var obj = new ChildChildClass();

        var ChildChildType = obj.GetType();

        var p = new Program();

        // here we get the ChildClass properties
        var t = p.getBaseType(ChildChildType, 1);
        Console.WriteLine(t.Name);
        p.getproperties(t);

        // here we get the BaseClass properties
        t = p.getBaseType(ChildChildType, 2);
        Console.WriteLine(t.Name);
        p.getproperties(t);

        // here we get the Object properties
        t = p.getBaseType(ChildChildType, 3);
        Console.WriteLine(t.Name);
        p.getproperties(t);

        Console.Read();
    }

    internal Type getBaseType(Type t, int level)
    {
        Type temp ;
        for (int i = 0; i < level; i++)
        {
            temp = t.BaseType;
            if(temp == null)
                throw new ArgumentOutOfRangeException("you are digging to deep");
            else
                t = temp;
        }

        return t;
    }

    private void getproperties(Type t)
    {
        PropertyInfo[] properties = t.GetProperties(BindingFlags.DeclaredOnly |
                                                    BindingFlags.Public |
                                                    BindingFlags.Instance);

        foreach (PropertyInfo property in properties)
        {
            Console.WriteLine(property.Name);
        }

        Console.WriteLine("");
    }
}

if you want some information about BindingFlags here is a Link

Community
  • 1
  • 1
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89