Skip to content

Instantly share code, notes, and snippets.

@nirinchev
Created February 2, 2022 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nirinchev/b9c614e39bbfe957e7954b6d1093ed10 to your computer and use it in GitHub Desktop.
Save nirinchev/b9c614e39bbfe957e7954b6d1093ed10 to your computer and use it in GitHub Desktop.
Recursive attribute declaration
var recursiveAtt = typeof(MyClass).GetCustomAttributes(inherit: true).Single() as RecursiveAttribute;
Console.WriteLine($"MyClass attribute value: {recursiveAtt.SomeValue}");
var recursiveAttConstructorAtt = recursiveAtt.GetType().GetConstructor(new Type[0]).GetCustomAttributes(inherit: true).Single() as RecursiveAttribute;
Console.WriteLine($"MyClass attribute's ctor's attribute value: {recursiveAttConstructorAtt.SomeValue}");
// Prints:
// MyClass attribute value: cde
// MyClass attribute's ctor's attribute value: abc
[Recursive(SomeValue = "cde")]
internal class MyClass { }
[AttributeUsage(AttributeTargets.All)]
internal class RecursiveAttribute : Attribute
{
public string SomeValue { get; set; }
[Recursive(SomeValue = "abc")]
public RecursiveAttribute()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment