You cannot if a third-party tool uses standard reflection to get attributes for your type.
You can if a third-party tool uses the TypeDescriptor
API to get attributes for your type.
Sample code for a type descriptor:
public class Foo { public string Bar { get; set; } } class FooMetadata { [Display(Name = "Bar")] public string Bar { get; set; } } static void Main(string[] args) { PropertyDescriptorCollection properties; AssociatedMetadataTypeTypeDescriptionProvider typeDescriptionProvider; properties = TypeDescriptor.GetProperties(typeof(Foo)); Console.WriteLine(properties[0].Attributes.Count);
If you run this code, you will see that the last console entry prints plus one attribute, because the Display
attribute is also considered now.
source share