代码之家  ›  专栏  ›  技术社区  ›  Tiago Crizanto

从PropertyInfo获取复杂类型属性

  •  -1
  • Tiago Crizanto  · 技术社区  · 8 年前

    我试图构建一个通用递归函数来迭代所有属性/复杂属性,并从以下结构返回所有属性的数组:

    public class Root
    {
       [FieldCodeItems(1, EnumFieldCode.INT, "ED", "0204")]
       public int Prop1 { get; set; }
       public Child Child { get; set; }
    }
    public class Child
    {
        [FieldCodeItems(1, EnumFieldCode.INT, "ED", "0208")]
        public int PropChild1 { get; set; }
        [FieldCodeItems(19, EnumFieldCode.ALPHANUMERIC, "ED", "0208")]
        public string PropChild2 { get; set; }
        public Child1 Child1 { get; set; }
    }
    
    public class Child1
    {
        [FieldCodeItems(1, EnumFieldCode.INT, "ED", "0211")]
        public int PropChild3 { get; set; }
    }
    
    public class MyReturClass
    {
        public string FileCode { get; set; }
        public string FieldCode { get; set; }
    }
    

    我可以从根类中读取所有属性,但无法从复杂属性中获取属性:

    public static List<MyReturClass> GetItems<T>(T obj)
    {
        var ret = new List<MyReturClass>();
    
        PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo property in properties)
        {
            IEnumerable<Attribute> attributes = property.GetCustomAttributes();
            foreach (Attribute attribute in attributes)
            {
                //here I read values from a custom property
                var tr = (FieldCodeItems)attribute;
                var value = obj.GetType().GetProperty(property.Name).GetValue(obj, null);
    
                if (value == null) continue;
    
                ret.Add(new MyReturClass
                {
                    FieldCode = tr.FieldCode,
                    FileCode = tr.FileCode
                });     
            }
            //If is complex object (Child, Child1 etc)
            if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
            {
                //I would like pass the complex property as parameter, but at this moment 
                //the property variable is a PropertyInfo and I need the complex type
                //to get properties values from this object
                GetItems(property); //Error. Also tried with GetItems(property.PropertyType);
            }
        }
    
        return ret;
    }
    

    我想将复杂属性作为参数传递,但此时属性变量是PropertyInfo,我需要复杂类型从该对象获取属性值。 我怎样才能得到这个东西?

    我找了 here , here 和 here 但解决方案并不能解决我的问题。

    编辑日期:2018年8月3日

    最后我找到了awswer here

    我添加了以下代码来解决这个问题:

    //If is complex object (Child, Child1 etc)
    if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
    {
        if (obj == null) { return null; }
    
        Type type = obj.GetType();
        PropertyInfo info = type.GetProperty(property.Name);
    
        if (info == null) { return null; }
        var v = info.GetValue(obj, null);
    
        if (v != null)
            GetItems(v);
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Aldert    8 年前

    看看 https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/how-to-examine-and-instantiate-generic-types-with-reflection

    我不确定你要找的东西是否有用,因为在你的EF中,你正在将类配置为ComplexType。我不确定这个类本身是否知道它是一个complexType。最终可能会在DBcontext上创建一个方法。。。