[英]C# - Serialize all attributes of derived class in a list of base class
[英]C# validation attributes for derived class
我有一些属性的基类:
public class A
{
[Display(Name = "Some Property")]
public virtual int SomeProperty {get;set;}
}
及其派生类:
public class B:A
{
[Required]
public override int SomeProperty {get;set;}
}
但是实际上,当我将类B的元数据提取到ModelMetadata时,IsRequired属性设置为false。 有没有一种方法可以应用Required属性,因此它反映在ModelMetadata中?
请尝试以下示例:
public class A
{
[Display(Name = "Some Property")]
public virtual int SomeProperty { get; set; }
}
public class B : A
{
[MyRequired]
public override int SomeProperty { get; set; }
}
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class MyRequiredAttribute : RequiredAttribute
{
}
最后:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.