particularly when overriding Asp.net MVC ValidationAttribute, is it possible to know class name which has field marked with attribute ? (AttributeUsage is AttributeTargets.Property)
public class UniqueAttribute: ValidationAttribute
public override bool IsValid(object value)
{
// how to know which class has this attribute?
}
Given an instance of an attribute it is not possible to determine what class, field, method, etc … originated this attribute. Primarily because it doesn’t need to be attached to one. All attributes can be created just like a normal object and hence not be attached to anything
You may need to use a different constructor but one must exist or it couldn’t be applied to a member in the first place.
It is possible to go the other way though. Given a member or type to find out if it has a given attribute applied to it. For example, assuming there is a single instance of
UnqiueAttributeattached to the typeSomeTypethis code will retrieve it.