let’s say I have 2 classes like this:
public class Foo
{
[Required]
public string Name {get;set;}
}
public class Bar
{
// put here [Required] at run-time
public string Name {get;set;}
}
var foo = new Foo();
var bar = new Bar();
//copy the required from foo to bar
is it possible to copy the attributes from foo to bar at run-time ?
The notion of “copying” attributes is out. However, you can do something meaningful in the code that checks if the attribute is applied. You could use another attribute that tells the code that it should use another type to verify for the [Required] attribute. For example:
Which you’d use like this:
The code that checks for the attribute could look like this:
Note how this code allows the attribute provider to be chained.