[英]c# Reflection, how to get generic type class properties
I have a Generic type List<Shift>
where我有一个通用类型
List<Shift>
在哪里
public class Shift
{
[DisplayFormat(DataFormatString = "dd-MMM-yyy '(hours)'")]
public DateTimeOffset Date { get; set; }
[DisplayFormat(DataFormatString = "hh':'mm")]
public TimeSpan TimeWorked { get; set; }
}
I'm trying to get schedule props with attributes using reflection我正在尝试使用反射获取具有属性的日程安排道具
var props = typeof(List<ShiftDayItem>).GetProperties();
var ShiftProperty = props.Last();
But ShiftProperty contains no attributes, so I can't access Date or TimeWorked.但是ShiftProperty不包含任何属性,因此我无法访问 Date 或 TimeWorked。 Is reflection not aware of these or is there another way for me to get those properties?
反射是不知道这些,还是有其他方法可以让我获得这些属性? Thanks!
谢谢!
I think you are misunderstanding what "Properties" are in c#.我认为您误解了 c# 中的“属性”是什么。 Your
Shift
class has 2 properties: "Date" and "TimeWorked".您的
Shift
class 有 2 个属性:“日期”和“工作时间”。 To get info about these 2 properties you can simply write: typeof(Shift).GetProperties()
.要获取有关这两个属性的信息,您可以简单地编写:
typeof(Shift).GetProperties()
。 You are calling typeof(List<ShiftDayItem>).GetProperties()
which will give you properties of List class: Count, Capacity, etc.. these are entirely unrelated.您正在调用
typeof(List<ShiftDayItem>).GetProperties()
,它将为您提供 List class 的属性:计数、容量等。这些完全不相关。
I think you want to get attributes from Shift
properties.我认为您想从
Shift
属性中获取属性。 To do this you need to get the generic parameter T
from List<T>
.为此,您需要从
List<T>
获取泛型参数T
To achive this you can do this为了实现这一点,你可以这样做
// Gets the generic type T from List<T>, in your case Shift
// Replace 'typeof(List<Shift>)' with your actual list
Type listType = typeof(List<Shift>).GenericTypeArguments[0];
To get all attributes from the type Shift
you first need to get all the properites from which you can get the attributes.要从
Shift
类型中获取所有属性,您首先需要获取可以从中获取属性的所有属性。
// Gets all properties from the generic type T
PropertyInfo[] shiftProperties = listType.GetProperties();
Now that you have all the properties you can get the attributes现在您拥有所有属性,您可以获取属性
// Gets all Attributes from each property and puts them in one list (SelectMany)
IEnumerable<Attribute> attributes = shiftProperties.SelectMany(prop => prop.GetCustomAttributes());
You get IEnumerable<Attribute>
from each property, which would result in IEnumerable<IEnumerable<Attribute>>
which is not what we want.您从每个属性中获取
IEnumerable<Attribute>
,这将导致IEnumerable<IEnumerable<Attribute>>
这不是我们想要的。 We used the method SelectMany
which takes each IEnumerable and flattens it into one.我们使用了
SelectMany
方法,它接受每个 IEnumerable 并将其展平为一个。 When you put everything together you get this当你把所有东西放在一起时,你会得到这个
// Gets the generic type T from List<T>, in your case Shift
// Replace 'typeof(List<Shift>)' with your actual list
Type listType = typeof(List<Shift>).GenericTypeArguments[0];
// Gets all properties from the generic type T
PropertyInfo[] shiftProperties = listType.GetProperties();
// Gets all Attributes from each property and puts them in one list (SelectMany)
IEnumerable<Attribute> attributes = shiftProperties.SelectMany(prop => prop.GetCustomAttributes());
You need more code:您需要更多代码:
Dim shifttprops= ShiftProperty.PropertyType.GetProperties()
For Each prop in shifttprops
Dim attrs = prop.CustomAttributes
Next
Note the use of CustomAttributes
instead of Attributes
注意使用
CustomAttributes
而不是Attributes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.