像这样的东西?如果我理解正确,您需要由属性修饰的类的所有属性。我不想包含 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
不会显示。