2

是否可以将属性及其关联属性作为集合或属性集获取?

我正在查看的对象的属性具有在 JSON.NET 中使用的属性,我想找出它们都是什么。之后,我将尝试找出其中哪些不为空。

这是一个示例对象:

[JsonObject]
    public class Conditions
    {
        [JsonProperty("opened_since")]
        public DateTime? OpenedSince { get; set; }
        [JsonProperty("added_until")]
        public DateTime? AddedUntil { get; set; }
        [JsonProperty("opened_until")]
        public DateTime? OpenedUntil { get; set; }
        [JsonProperty("archived_until")]
        public DateTime? ArchivedUntil { get; set;
    }
4

4 回答 4

5

这将为您提供所有属性、它们的属性和属性参数的值(请注意,此解决方案假定您使用的是 .net 框架版本 4.5):

PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
    Console.WriteLine("Property: " + prop.Name);
    foreach (CustomAttributeData att in prop.CustomAttributes)
    {
        Console.WriteLine("\tAttribute: " + att.AttributeType.Name);
        foreach (CustomAttributeTypedArgument arg in att.ConstructorArguments)
        {
            Console.WriteLine("\t\t" + arg.ArgumentType.Name + ": " + arg.Value);
        }
    }
}

输出:

Property: OpenedSince
        Attribute: JsonPropertyAttribute
                String: opened_since
Property: AddedUntil
        Attribute: JsonPropertyAttribute
                String: added_until
Property: OpenedUntil
        Attribute: JsonPropertyAttribute
                String: opened_until
Property: ArchivedUntil
        Attribute: JsonPropertyAttribute
                String: archived_until
于 2013-03-21T03:48:38.740 回答
2

试试这个

public static Hashtable ConvertPropertiesAndValuesToHashtable(this object obj)
    {
        var ht = new Hashtable();

        // get all public static properties of obj type
        PropertyInfo[] propertyInfos =
            obj.GetType().GetProperties().Where(a => a.MemberType.Equals(MemberTypes.Property)).ToArray();
        // sort properties by name
        Array.Sort(propertyInfos, (propertyInfo1, propertyInfo2) => propertyInfo1.Name.CompareTo(propertyInfo2.Name));

        // write property names
        foreach (PropertyInfo propertyInfo in propertyInfos)
        {
            ht.Add(propertyInfo.Name,
                   propertyInfo.GetValue(obj, BindingFlags.Public, null, null, CultureInfo.CurrentCulture));
        }

        return ht;
    }
于 2013-03-21T02:56:00.553 回答
2

在阅读了上面 Chris 的回答和Marc Gravell 对这个问题的回答后,我找到了答案

我创建了这个函数来为我做这件事:

    public HastTable BuildRequestFromConditions(Conditions conditions)
    {
        var ht = new HashTable();
        var properties = conditions.GetType().GetProperties().Where(a => a.MemberType.Equals(MemberTypes.Property) && a.GetValue(conditions) != null);
        properties.ForEach(property => 
            {
                var attribute = property.GetCustomAttribute(typeof(JsonPropertyAttribute));
                var castAttribute = (JsonPropertyAttribute)attribute;

                ht.Add(castAttribute.PropertyName, property.GetValue(conditions));
            });
        return request;
    }
于 2013-03-21T03:39:31.010 回答
1

像这样的东西?如果我理解正确,您需要由属性修饰的类的所有属性。我不想包含 JSON.NET 参考,所以改用XmlText属性。

    [Test]
    public void dummy()
    {
        var conditions = new Conditions();

        var propertyInfos = conditions.GetType().GetProperties();
        propertyInfos.ForEach(x =>
            {
                var attrs = x.GetCustomAttributes(true);
                if (attrs.Any(p => p.GetType() == typeof(XmlTextAttribute)))
                {
                    Console.WriteLine("{0} {1}", x, attrs.Aggregate((o, o1) => string.Format("{0},{1}",o,o1)));
                }
            });
    }

班级看起来像这样 -

[XmlType]
public class Conditions
{
    [XmlText]
    public DateTime? OpenedSince { get; set; }

    [XmlText]
    public DateTime? AddedUntil { get; set; }

    [XmlText]
    public DateTime? OpenedUntil { get; set; }

    [XmlText]
    public DateTime? ArchivedUntil { get; set; }

    public string NotTobeListed { get; set; }
}

控制台输出:

System.Nullable`1[System.DateTime] OpenedSince System.Xml.Serialization.XmlTextAttribute
System.Nullable`1[System.DateTime] AddedUntil System.Xml.Serialization.XmlTextAttribute
System.Nullable`1[System.DateTime] OpenedUntil System.Xml.Serialization.XmlTextAttribute
System.Nullable`1[System.DateTime] ArchivedUntil System.Xml.Serialization.XmlTextAttribute

请注意,NotToBeListed不会显示。

于 2013-03-21T03:17:56.413 回答