如何获取对象中存在的所有日期时间类型?从对象中获取特定类型
E.G.装运对象包含有关装运的所有详细信息,如托运人名称,收货人等。它还包含许多日期时间字段,例如收到日期,运输日期,交货日期等。
如何获取所有日期字段装运对象?
如何获取对象中存在的所有日期时间类型?从对象中获取特定类型
E.G.装运对象包含有关装运的所有详细信息,如托运人名称,收货人等。它还包含许多日期时间字段,例如收到日期,运输日期,交货日期等。
如何获取所有日期字段装运对象?
您可以使用反射。
Type myClassType = typeof(MyClass); // or object.GetType()
var dateTimeProperties = from property in myClassType.GetProperties()
where property.PropertyType == typeof(DateTime)
select property;
约反射在.NET多个读
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx
http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.aspx
井的最简单的方法是直接访问属性例如
var receivedDate = shipment.ReceivedDate;
var transportedDate = shipment.DeliveryDate;
...
另一种做法是让你的Shipment
对象例如返回列表你
public Dictionary<string, DateTime> Dates
{
get
{
return new Dictionary<string, DateTime>()
{
new KeyValuePair<string, DateTime>("ReceivedDate", ReceivedDate),
new KeyValuePair<string, DateTime>("DeliveryDate", DeliveryDate),
...
}
}
}
...
foreach (var d in shipment.Dates)
{
Console.WriteLine(d.Key, d.Value);
}
或者最后,使用反射来遍历属性:
public Dictionary<string, DateTime> Dates
{
get
{
return from p in this.GetType().GetProperties()
where p.PropertyType == typeof(DateTime)
select new KeyValuePair<string, DateTime>(p.Name, (DateTime)p.GetValue(this, null));
}
}
请告诉我你的代码呢? – BugFinder