【发布时间】:2015-04-24 20:00:17
【问题描述】:
我有一个这样的类和接口:
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;}
}
然后我继承了这个类:
public class MenuItem : Nav_Item
{
public virtual IEnumerable<MenuItem> Children { get; set; }
//some other properties
}
我现在正在尝试实例化MenuItem 类型的对象,并尝试从继承类中获取属性(我无法直接实例化MenuItem,因为该类型是从其他类传入的)
object obj = Activator.CreateInstance(type);
foreach (PropertyInfo propInfo in type.GetProperties())
{
FieldAttribute sfi =(FieldAttribute)propInfo.PropertyType.GetCustomAttribute(typeof(FieldAttribute));
}
但这让我sfi 成为null。我也调试过尝试获取所有属性:
propInfo.PropertyType.GetCustomAttributes()
.. 但这只是给我系统属性(类型和其他东西),但我自己的属性不在那里?这是因为类是继承的吗?如何获取属性值?
编辑:
属性类定义如下:
public class FieldIDAttribute : Attribute
{
private string _id;
public FieldAttribute(string Id)
{
_id = Id;
}
public ID TheFieldID
{
get
{
return new ID(_id);
}
}
}
【问题讨论】:
标签: c# asp.net asp.net-mvc reflection attributes