class Thing { [SomeAttribute] public float a, b, c, d; }
The above that you proposed will work the way you expect it to work. You can check this out:
[AttributeUsage(AttributeTargets.Field)] sealed class SomeAttribute: Attribute { public SomeAttribute() { } } class Program { static void Main(string[] args) { var t = typeof(Thing); var attrs = from f in t.GetFields() from a in f.GetCustomAttributes() select new { Name = f.Name, Attribute = a.GetType() }; foreach (var a in attrs) Console.WriteLine(a.Name + ": " + a.Attribute); Console.ReadLine(); } }
He prints:
a: SomeAttribute
b: SomeAttribute
c: SomeAttribute
d: SomeAttribute
source share