简体   繁体   English

从PropertyInfo获取复杂的类型属性

[英]Get complex type properties from PropertyInfo

I trying to build a generic recursive function to iterate all properties / complex properties and return an array of all properties from the following structre: 我试图构建一个通用的递归函数来迭代所有属性/复杂属性,并从以下structre返回所有属性的数组:

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; }
}

I can read all properties from Root class but I can't get the properties from the complex properties: 我可以从Root类读取所有属性,但是无法从复杂属性中获取属性:

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;
}

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. 我想将复杂的属性作为参数传递,但是目前属性变量是PropertyInfo,我需要复杂的类型才能从该对象获取属性值。 How can I get this object? 我怎样才能得到这个物体?

I searched here , here and here but the solutions don't solve my problem. 在这里这里这里搜索,但是解决方案不能解决我的问题。

Edit - 03-08-2018 编辑-2018/03/08

Finally I found the awswer here 终于我在这里找到了

I added this code to solve the problem: 我添加了以下代码来解决问题:

//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);
}

have a look at https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/how-to-examine-and-instantiate-generic-types-with-reflection 看看https://docs.microsoft.com/zh-cn/dotnet/framework/reflection-and-codedom/how-to-examine-and-instantiate-generic-types-with-reflection

I am not sure if what you are looking for is going to work because in you EF you are configuring the classes as ComplexType. 我不确定您要寻找的内容是否会正常工作,因为在您的EF中,您将类配置为ComplexType。 I am not sure the class itself knows its a complexType. 我不确定类本身是否知道其complexType。 You might end up creating a method on the DBcontext... 您可能最终在DBcontext上创建了一个方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM