无法从 PropertyInfo c# 获取属性
Cannot get attribute from PropertyInfo c#
我有一个 class 界面设置如下:
public partial interface INav_Item
{
[FieldID("df918977-369c-4a06-ac38-adb8741b5f75")]
string Title {get; set;}
}
public partial class Nav_Item : INav_Item
{
[FieldID("df918977-369c-4a06-ac38-adb8741b5f75")]
public virtual string Title {get; set;}
}
然后我继承了这个 class:
public class MenuItem : Nav_Item
{
public virtual IEnumerable<MenuItem> Children { get; set; }
//some other properties
}
我现在正在尝试实例化类型为 MenuItem
的对象,并尝试从 Inherited class 中获取属性(我无法直接实例化 MenuItem
,因为该类型是从其他 classes)
传入的
object obj = Activator.CreateInstance(type);
foreach (PropertyInfo propInfo in type.GetProperties())
{
FieldAttribute sfi =(FieldAttribute)propInfo.PropertyType.GetCustomAttribute(typeof(FieldAttribute));
}
但这让我 sfi
成为 null
。我还进行了调试以尝试获取所有属性:
propInfo.PropertyType.GetCustomAttributes()
..但这只是给我系统属性(类型和其他东西),但我自己的属性不存在?这是因为 class 是继承的吗?如何获取属性值?
编辑:
属性 class 定义如下:
public class FieldIDAttribute : Attribute
{
private string _id;
public FieldAttribute(string Id)
{
_id = Id;
}
public ID TheFieldID
{
get
{
return new ID(_id);
}
}
}
不,这不是继承问题。你需要改变这个:
FieldAttribute sfi =(FieldAttribute)propInfo.PropertyType
.GetCustomAttribute(typeof(FieldAttribute));
对此:
var sfi = propInfo.GetCustomAttribute(typeof(FieldIDAttribute)) as FieldIDAttribute;
这是因为 PropertyType
returns 属性 的类型,即您的 string
。类型 string
没有您的自定义属性。
您对 FieldIDAttribute
class 的编辑也不正确。它的构造函数与 class 名称不匹配,并且它具有未声明且不正确的 ID
类型。
除了 Alex 所说的,您还应该指定绑定标志,这对我有用:
foreach (PropertyInfo propInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
FieldIDAttribute sfi = propInfo.PropertyType.GetCustomAttribute(typeof(FieldIDAttribute)) as FieldIDAttribute;
//Check if null here, if not, it has the attribute.
}
编辑:"type" 必须是 Nav_Item,您没有将 FieldIDAttribute 应用于 Menu_item class 中的任何内容。为了获得 IEnumerable 的属性,您必须枚举它并读取列表中每种类型元素的属性。
我有一个 class 界面设置如下:
public partial interface INav_Item
{
[FieldID("df918977-369c-4a06-ac38-adb8741b5f75")]
string Title {get; set;}
}
public partial class Nav_Item : INav_Item
{
[FieldID("df918977-369c-4a06-ac38-adb8741b5f75")]
public virtual string Title {get; set;}
}
然后我继承了这个 class:
public class MenuItem : Nav_Item
{
public virtual IEnumerable<MenuItem> Children { get; set; }
//some other properties
}
我现在正在尝试实例化类型为 MenuItem
的对象,并尝试从 Inherited class 中获取属性(我无法直接实例化 MenuItem
,因为该类型是从其他 classes)
object obj = Activator.CreateInstance(type);
foreach (PropertyInfo propInfo in type.GetProperties())
{
FieldAttribute sfi =(FieldAttribute)propInfo.PropertyType.GetCustomAttribute(typeof(FieldAttribute));
}
但这让我 sfi
成为 null
。我还进行了调试以尝试获取所有属性:
propInfo.PropertyType.GetCustomAttributes()
..但这只是给我系统属性(类型和其他东西),但我自己的属性不存在?这是因为 class 是继承的吗?如何获取属性值?
编辑:
属性 class 定义如下:
public class FieldIDAttribute : Attribute
{
private string _id;
public FieldAttribute(string Id)
{
_id = Id;
}
public ID TheFieldID
{
get
{
return new ID(_id);
}
}
}
不,这不是继承问题。你需要改变这个:
FieldAttribute sfi =(FieldAttribute)propInfo.PropertyType
.GetCustomAttribute(typeof(FieldAttribute));
对此:
var sfi = propInfo.GetCustomAttribute(typeof(FieldIDAttribute)) as FieldIDAttribute;
这是因为 PropertyType
returns 属性 的类型,即您的 string
。类型 string
没有您的自定义属性。
您对 FieldIDAttribute
class 的编辑也不正确。它的构造函数与 class 名称不匹配,并且它具有未声明且不正确的 ID
类型。
除了 Alex 所说的,您还应该指定绑定标志,这对我有用:
foreach (PropertyInfo propInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
FieldIDAttribute sfi = propInfo.PropertyType.GetCustomAttribute(typeof(FieldIDAttribute)) as FieldIDAttribute;
//Check if null here, if not, it has the attribute.
}
编辑:"type" 必须是 Nav_Item,您没有将 FieldIDAttribute 应用于 Menu_item class 中的任何内容。为了获得 IEnumerable 的属性,您必须枚举它并读取列表中每种类型元素的属性。