Sebastián Guerrero
Sebastián Guerrero

Reputation: 1114

Multiple Attributes in one?

I have these properties (A,B) :

[DataType(DataType.MultilineText)]
[Required(ErrorMessage = "A is required"), DisplayName("A")]
[StringLength(Constants.MaximunStringSize)]
public string A { get; set; }

[DataType(DataType.MultilineText)]
[Required(ErrorMessage = "B is required"), DisplayName("B")]
[StringLength(Constants.MaximunStringSize)]
public string B { get; set; }

I can create a class that "inherits" all the attributes (DataType, Required, StringLength, DisplayName) and the set through its constructor?. By example:

[MyAttribute("A","A is required")]
public string A { get; set; }

[MyAttribute("B","B is required")]
public string B { get; set; }

Upvotes: 3

Views: 3077

Answers (2)

Kyle Trauberman
Kyle Trauberman

Reputation: 25684

There is no multiple inheritance in C#, so no, you can't do this.

You can, however write your own Attribute that incorporates all the functionality of those attributes.

Upvotes: 2

SLaks
SLaks

Reputation: 887225

In general, no.

However, for validation attributes, you could create your own validation attribute that contains all of the logic in the existing attributes.

To emulate [DataType], you'll need to implement IMetadataAware.

Upvotes: 3

Related Questions