![](/img/trans.png)
[英]How to dynamically use the PropertyType reflection attribute to create a respective typed Function<>?
[英]C# Attribute PropertyType Reflection
我正在尝试从属性构造函数中确定与属性相关的属性的类型。 更具体的我正在寻找包含该属性的类。
我当前的构造函数如下所示:
public IndexedCategoryAttribute(Type DefiningClass, String HeaderText, int Index)
{
this._definingClass = DefiningClass;
但是我希望它看起来像:
public IndexedCategoryAttribute(String HeaderText, int Index)
{
PropertyInfo Info = ???
this._definingClass = Info.DeclaringType;
我相信这将为该属性的用户提供更安全的代码,因为目前它在Attribute定义中需要一个'typeof(MyClass)',从而打开了所提供错误类型的可能性?
有关更多信息,我将其与propertygrid一起使用。 在static Dictionary<Type, Dictionary<String, int>>
使用该static Dictionary<Type, Dictionary<String, int>>
以将类别分组到与它们相关的类。
我不认为实例化属性时,不能知道它们所关联的类型或属性。
这种关系是相反的:
属性没有“拥有”类型。
在构建网格时,应该以与建议相反的方向进行扫描。 对于集合中的每种类型,您可以进行反思以确定该类型所附加的属性(或深入研究该属性的属性),如果这些属性是您的类别属性,请添加到字典中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.