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:
[AttributeUsage(AttributeTargets.Class)]
public class AttributeProviderAttribute : Attribute {
public AttributeProviderAttribute(Type t) { Type = t; }
public Type Type { get; set; }
}
Which you'd use like this:
public class Foo {
[Required]
public string Name { get; set; }
}
[AttributeProvider(typeof(Foo))]
public class Bar {
public string Name { get; set; }
}
The code that checks for the attribute could look like this:
static bool IsRequiredProperty(Type t, string name) {
PropertyInfo pi = t.GetProperty(name);
if (pi == null) throw new ArgumentException();
// First check if present on property as-is
if (pi.GetCustomAttributes(typeof(RequiredAttribute), false).Length > 0) return true;
// Then check if it is "inherited" from another type
var prov = t.GetCustomAttributes(typeof(AttributeProviderAttribute), false);
if (prov.Length > 0) {
t = (prov[0] as AttributeProviderAttribute).Type;
return IsRequiredProperty(t, name);
}
return false;
}
Note how this code allows the attribute provider to be chained.