简体   繁体   English

c# 反射,如何获得泛型类型 class 属性

[英]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.

相关问题 通用 C# - 如何使用反射将属性分配给通用 class - Generic C# - how to use reflection to assign properties to generic class 如何使用C#反射获取实例化的属性或不为null的类类型的属性 - How to use C# reflection to get properties that are instantiated or properties of a class type that are not null C#如何获取泛型类型属性 - C# how to get Generic type properties 如何使用反射C#获取构造函数泛型参数类型 - How to get Constructor Generic parameter type using reflection c# 获取C#反射中的属性类型 - get type of properties in C# reflection 反射C#-如何获取对几个属性进行深入分类的句柄 - Reflection C# - How to get handle to class several properties deep 从Type with Reflection获取类并使用Type in C#调用泛型构造函数 - Get class from Type with Reflection and call a generic constructor with Type in C# C# 反射 - 从基类中获取超类的通用参数类型 - C# Reflection - Get Generic Argument type of Super class from within the Base class C#反射获取某个类的所有子类并用它们的类型调用泛型方法 - C# reflection get all subclasses of certain class and call generic method with their type c# 获取属性类型是否继承自一个带反射的抽象泛型 class - c# Get if property type inherits from one abstract generic class with reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM