Can somebody explain to me why Value.GetType().GetCustomAttribute
returns null
? I have looked at ten different tutorials on how to get the attributes for an enumerated type member. No matter which GetCustomAttribute*
method I use, I get no custom attributes returned.
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;
}
}