Generic attributes
One of the limitations of C# attributes that we used to have is that attributes could not be generic types taking generic arguments. Prior to C# 11, you would get a compiler error if you added a generic argument to your attribute class. This limitation is lifted with the release of C# 11.
Up till C# 11, the only way you could collect type information was for the attribute to have parameters or properties that were of type System.Type. This became very verbose:
public class CustomAttribute : Attribute { public CustomAttribute(Type theType) }
And then adorning a type with the attribute would be as follows:
[Custom(typeof(string))] public class MyClass { }
With C# 11, now you can improve how you get type information:
public class CustomAttribute<T> : Attribute { }
When you adorn a type with the attribute, you use the generic argument:
[Custom<string>] public class MyClass { }
If you’re looking to...