【问题标题】:Get complex type properties from PropertyInfo从 PropertyInfo 获取复杂类型属性
【发布时间】:2018-08-04 01:03:13
【问题描述】:

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

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

我可以从 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;
}

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

我搜索了hereherehere,但解决方案并没有解决我的问题。

编辑 - 03-08-2018

终于找到了here的awswer

我添加了这段代码来解决问题:

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

【问题讨论】:

    标签: c# system.reflection


    【解决方案1】:

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

    我不确定您正在寻找的内容是否会起作用,因为在您的 EF 中,您将类配置为 ComplexType。我不确定这个类本身是否知道它是一个 complexType。您最终可能会在 DBcontext 上创建一个方法...

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2023-03-13
    • 2022-01-12
    相关资源
    最近更新 更多