なぜValue.GetType().GetCustomAttribute
戻るのか誰か説明してもらえますnull
か?列挙型メンバーの属性を取得する方法について、10 個の異なるチュートリアルを見てきました。どのGetCustomAttribute*
方法を使用しても、カスタム属性が返されません。
using System;
using System.ComponentModel;
using System.Reflection;
public enum Foo
{
[Bar(Name = "Bar")]
Baz,
}
[AttributeUsage(AttributeTargets.Field)]
public class BarAttribute : Attribute
{
public string Name;
}
public static class FooExtensions
{
public static string Name(this Foo Value)
{
return Value.GetType().GetCustomAttribute<BarAttribute>(true).Name;
}
}