I have a custom attribute, inside the constructor of my custom attribute I want to set the value of a property of my attribute to the type of the property my attribute was applied to, is there someway to access the member that the attribute was applied to from inside my attribute class?
Asked
Active
Viewed 8,448 times
24
-
Can you briefly describe the use-case? – Tanmay Feb 19 '10 at 20:10
-
1If you can provide more detail to the problem you are solving, it might be possible to provide an alternative solution. – Paul Turner Feb 19 '10 at 20:13
-
1Thanks, I know how I can achieve the same in a different way, but I wanted to know if this was possible because the code would be cleaner. – ryudice Feb 19 '10 at 20:46
3 Answers
20
It's possible from .NET 4.5 using CallerMemberName
:
[SomethingCustom]
public string MyProperty { get; set; }
Then your attribute:
[AttributeUsage(AttributeTargets.Property)]
public class SomethingCustomAttribute : Attribute
{
public StartupArgumentAttribute([CallerMemberName] string propName = null)
{
// propName == "MyProperty"
}
}

Niels Filter
- 4,430
- 3
- 28
- 42
-
1This should be the marked answer now! Exactly what I needed, thank you! – benscabbia Apr 26 '18 at 05:51
-
2This was added some time ago, but I have a follow up question: According to this link [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information#member-names] [CallerMemberName] only gives you the member name, not in a fully qualified way. So if I want to get that type via reflection, how can I know it's full name? – ohnezahn Oct 22 '19 at 15:00
-
There's no alternative at the moment for getting the calling type. Hopefully this will come through soon. See discussion here: https://github.com/dotnet/csharplang/discussions/87 – Niels Filter Jul 12 '22 at 14:49
18
Attributes don't work that way, I'm afraid. They are merely "markers", attached to objects, but unable to interact with them.
Attributes themselves should usually be devoid of behaviour, simply containing meta-data for the type they are attached to. Any behaviour associated with an attribute should be provided by another class which looks for the presence of the attribute and performs a task.
If you are interested in the type the attribute is applied to, that information will be available at the same time you are reflecting to obtain the attribute.

Paul Turner
- 38,949
- 15
- 102
- 166
-
As you say, it's not the end of the world as you know the type when getting the custom attribute via Reflection but it would be "nice" if the Type passed in to GetCustomAttribute was also stored in the System.Attribute – Rob Nicholson Apr 28 '15 at 08:59
0
You can do next. It is simple example.
//target class
public class SomeClass{
[CustomRequired(ErrorMessage = "{0} is required", ProperytName = "DisplayName")]
public string Link { get; set; }
public string DisplayName { get; set; }
}
//custom attribute
public class CustomRequiredAttribute : RequiredAttribute, IClientValidatable
{
public string ProperytName { get; set; }
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var propertyValue = "Value";
var parentMetaData = ModelMetadataProviders.Current
.GetMetadataForProperties(context.Controller.ViewData.Model, context.Controller.ViewData.Model.GetType());
var property = parentMetaData.FirstOrDefault(p => p.PropertyName == ProperytName);
if (property != null)
propertyValue = property.Model.ToString();
yield return new ModelClientValidationRule
{
ErrorMessage = string.Format(ErrorMessage, propertyValue),
ValidationType = "required"
};
}
}

Klyuch
- 66
- 1
- 6