Created
February 2, 2022 15:27
-
-
Save nirinchev/b9c614e39bbfe957e7954b6d1093ed10 to your computer and use it in GitHub Desktop.
Recursive attribute declaration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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