是否有可能使用基类在派生类中的overriden属性上应用某个属性? 比方说,我有一个Person类和一个继承自Person的Person类。另外,为personForm具有一个在它的属性之一使用的属性(比如说MyAttribute),这是从基础,人,类重写:使用基类反映派生类中的属性
public class Person
{
public virtual string Name { get; set; }
}
public class PersonForm : Person
{
[MyAttribute]
public override string Name { get; set; }
}
public class MyAttribute : Attribute
{ }
现在我有我的项目是一个通用的保存功能那将在一刻接收Person类型的对象。 问题是:在使用Person对象时,是否可以从派生的PersonForm中看到MyAttribute?
在现实世界中,这发生在MVC应用程序中,我们使用PersonForm作为显示表单的类,Person类作为Model类。当来到Save()方法时,我得到了Person类。但属性在PersonForm类中。
[Attribute.GetCustomAttributes Method(MemberInfo,Boolean)](http://msdn.microsoft.com/en-us/library/ms130868(v = vs.110).aspx)将第二个参数设置为true。 – Yuriy 2014-09-04 16:48:21