scott
scott

Reputation: 974

Is it possible for a subclass to override the attributes of the base class?

For example in in asp mvc,

public class BaseModel
{
    [DisplayName("Such a pretty name")]
    public virtual String TheName {get;set;}
}

public class SubModel : BaseModel
{
    [DisplayName("An even prettier name!")]
    public override string TheName {get;set;}
}

With the above would it be possible to get the Views that use SubModel to make use of its display name attribute and not the base model's?

When I pass submodel to a view, the label isn't using either of the displayname attributes, just the property name.

Upvotes: 5

Views: 2066

Answers (1)

user885100
user885100

Reputation:

Take a look at AttributeUsage. IT should allow you to limit the scope of the attributes in your base class.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class BaseClass

Upvotes: 1

Related Questions