c# - 尝试查找给定深层嵌套类/对象的属性名称

标签 c# list object nested getproperty

我正在研究一个包含字符串格式“值”的对象,该对象与嵌套在我从中获取所述值的对象中的对象和属性相对应。 IE。如果我的对象包含一个嵌套对象列表,其中包含,比方说,名称,在联系人对象中,那么我有一个“值”,它可能读作类似于“ContactInfo[0].Name”的内容。

如何验证属性是否存在?我很确定,如果我能弄清楚这第一部分,那么我就可以轻松获得值(value)。

我试过使用类似的东西:

public static bool HasProperty(this object obj, string propertyName)
{
    return obj.GetType().GetProperty(propertyName) != null;
}

参数分别为“MasterInfo”、“Keys[0].KeyWord”和“Keys[1].Keyword”。

我已经尝试通过 DOT 表示法将它们分解为 segements 和我能想到的所有其他内容,但我似乎无法成功地使用方法中的关键字。始终为假。

我尝试通过按“.”分解关键字的例程来运行 MasterInfo 对象。并遍历每个部分,但结果没有变化...始终为 False。

非常欢迎新鲜的眼睛!我一定是错过了一些简单的和/或就在我面前的东西......

// simplified object example
public class MasterInfo
{
    public int Id { get; set; }

    public List<ContactInfo> Contacts {get; set;}

    public Message MessageDetail { get; set; }

    public List<Key> Keys { get; set; }
}

public class ContactInfo
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }
}

public class Message
{
    public int Id { get; set; }

    public string MessageContent { get; set; }
}

public class Key
{
    public int Id { get; set; }

    public string KeyWord { get; set; }
}



// serialized JSON of what the MasterInfo class would be populated with, easier to read than with '.' notation
{
    "Id" : 1,
    "Contacts" : [
        {
            "Id" : 1,
            "Name" : "Beavis",
            "Email" : "beavis@asd.asd"
        }
    ],
    "MessageDetail" : {
        "Id" : 23,
        "MessageContent" : "Hello, %%Contacts[0].Name%%, this was sent to you at %%Contacts[0].Email%%"
    },
    "Keys" : [
        {
            "Id" : 1,
            "KeyWord" : "Contacts[0].Name"
        },
        {
            "Id" : 2,
            "KeyWord" : "Contacts[0].Email"
        }
    ]
}



// method I'm trying to use to verify the keyword (property) exists before attempting to get it's value...
public static bool HasProperty(this object obj, string propertyName)
{
    return obj.GetType().GetProperty(propertyName) != null;
}

到目前为止,评估时一切都变成了错误。我很确定这与我正在深入研究嵌套对象这一事实有关,但此时我不确定任何事情。

最佳答案

    public static bool HasProperty(this object obj, params string[] properties)
    {
        return HasProperty(obj.GetType(), properties);
    }

    public static bool HasProperty(this Type type, params string[] properties)
    {
        if (properties.Length == 0) // if done properly, shouldn't need this
            return false;

        var propertyInfo = type.GetProperty(properties[0]);
        if (propertyInfo != null)
        {
            if (properties.Length == 1)
                return true;
            else // need to check the next level...
            {
                Type innerType = propertyInfo.PropertyType.GetGenericArguments().FirstOrDefault();
                if (innerType != null)
                    return HasProperty(innerType, properties.Skip(1).ToArray());
                else
                    return false;
            }
        }
        else
            return false;
    }

    public static void Testing()
    {
        MasterInfo masterInfo = new MasterInfo();
        Console.WriteLine(HasProperty(masterInfo, "Id")); // true
        Console.WriteLine(HasProperty(masterInfo, "Contacts", "Name")); // true
        Console.WriteLine(HasProperty(masterInfo, "Contacts", "Address")); // false
    }

关于c# - 尝试查找给定深层嵌套类/对象的属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57504564/

相关文章:

python - 在另一个列表中附加一个列表

linux - 如何从文件中确定优化级别?

c# - 如何在打开时使用 C# 和 Excel 2010 保存 Excel 文件而不收到格式警告

python - 如何在 python 中以更有效的方式重写这个循环

python - 将列读入单独的列表中

java - 如何将对象从一台计算机发送到另一台计算机?

java - 查找要添加对象的 ArrayList 索引的方法

c# - 自签名证书是否足够安全?

c# - 异常不从 C# 中的反射方法调用传播

c# - 遍历 LINQ 结果列表