CSharp · Console
// C# Extension Method
// Doc: https://csharp-extension.com/en/method/1002724/object-getcustomattributes
// @nuget: Z.ExtensionMethods
using System;
public class Program
{
public static void Main()
{
var thisClass = new MyTestClass();
var thisEnum = MyTestEnum.Test;
//C# Extension Method: Object - GetCustomAttributes
var result1 = thisClass.GetCustomAttributes<MyTestAttribute>();
var result2 = thisClass.GetCustomAttributes<MyTestAttribute>(false);
var result3 = thisClass.GetType().GetCustomAttributes<MyTestAttribute>();
var result4 = thisClass.GetType().GetCustomAttributes<MyTestAttribute>(false);
var result5 = thisClass.GetCustomAttributes();
var result6 = thisClass.GetCustomAttributes(false);
var result7 = thisEnum.GetCustomAttributes<MyTestAttribute>();
var result8 = thisEnum.GetCustomAttributes<MyTestAttribute>(false);
var result9 = thisEnum.GetCustomAttributes();
var result10 = thisEnum.GetCustomAttributes(false);
Console.WriteLine(result1[0].Text);
Console.WriteLine(result2[0].Text);
Console.WriteLine(result3[0].Text);
Console.WriteLine(result4[0].Text);
Console.WriteLine(((MyTestAttribute) result5[0]).Text);
Console.WriteLine(((MyTestAttribute) result6[0]).Text);
Console.WriteLine(result7[0].Text);
Console.WriteLine(result8[0].Text);
Console.WriteLine(((MyTestAttribute) result9[0]).Text);
Console.WriteLine(((MyTestAttribute) result10[0]).Text);
}
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class MyTestAttribute : Attribute
{
public string Text;
public MyTestAttribute(string text)
{
Text = text;
}
}
[MyTest("Test Class")]
public class MyTestClass
{
}
public enum MyTestEnum
{
[MyTest("Test Enum")]
Test
}