Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; }
like image 388
Sebastián Guerrero Avatar asked Nov 28 '25 04:11

Sebastián Guerrero


2 Answers

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.

like image 71
SLaks Avatar answered Nov 30 '25 16:11

SLaks


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.

like image 23
Kyle Trauberman Avatar answered Nov 30 '25 16:11

Kyle Trauberman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!