是否可以将属性及其相关属性设置为集合或属性集?
我正在查看的对象的属性具有在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;
}
答案 0 :(得分:3)
这将为您提供属性的所有属性,属性和参数值(请注意,此解决方案假定您使用的是.net framework 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
答案 1 :(得分: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;
}
答案 2 :(得分:2)
在阅读了上面的Chris'和Marc Gravell's answer to this question
后,我想出了答案我创建了这个函数来为我做这件事:
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;
}
答案 3 :(得分: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
未显示。