-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generic versions of attributes in ASP.NET Core #37767
Comments
WHAT!! how could I have missed this!! This is great! |
There may still be some issues with the implementation it turns out: dotnet/csharplang#124 (comment) |
Did a regex search with
I have excluded the attributes that are part of the tests project. |
@rafikiassumani-msft moving this over to your area path. FYI |
This could prevent wrong code from being compiled. Currently you get an exception if the parameter binderType does not implement the interface IModelBinder 👎 |
Triage: @brunolins16 will take the content from this comment and craft an API proposal template for it. |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API Review Notes:
API Approved! namespace Microsoft.AspNetCore.Mvc;
+[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
+public class ModelMetadataTypeAttribute<T> : ModelMetadataTypeAttribute
+{
+ public ModelMetadataTypeAttribute() : base(typeof(T))
+ { }
+}
+[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
+public class MiddlewareFilterAttribute<T> : MiddlewareFilterAttribute
+{
+ public MiddlewareFilterAttribute() : base(typeof(T))
+ { }
+}
+[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
+[System.Diagnostics.DebuggerDisplay("ServiceFilter: Type={ServiceType} Order={Order}")]
+public class ServiceFilterAttribute<TFilter> : ServiceFilterAttribute
+ where TFilter : IFilterMetadata
+{
+ public ServiceFilterAttribute() : base(typeof(TFilter))
+ { }
+}
+[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
+public class ModelBinderAttribute<TBinder> : ModelBinderAttribute
+ where TBinder : IModelBinder
+{
+ public ModelBinderAttribute() : base(typeof(TBinder))
+ { }
+}
+[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
+public class ProducesAttribute<T> : ProducesAttribute
+{
+ public ProducesAttribute() : base(typeof(T))
+ { }
+}
+[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
+public class ProducesResponseTypeAttribute<T> : ProducesResponseTypeAttribute
+{
+ public ProducesResponseTypeAttribute(int statusCode) : base(typeof(T), statusCode)
+ { }
+ public ProducesResponseTypeAttribute(int statusCode, string contentType, params string[] additionalContentTypes)
+ : base(typeof(T), statusCode, contentType, additionalContentTypes)
+ { }
+}
|
While working on implementing this, I found one more type that needed generic attribute overloads. +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
+public class TypeFilterAttribute<TFilter> : TypeFilterAttribute where TFilter : IFilterMetadata
+{
+ public TypeFilterAttribute() : base(typeof(T)) { }
+} |
Looks like it needs to be constrained to |
Indeed! I updated the comment to reflect this. |
Background and Motivation
Now that C# supports generic attributes as of version 10/.NET 6, we should look through ASP.NET Core and consider adding generic versions of any attributes that accept type parameters (e.g. ConsumesAttribute).
Proposed API
In addition to what listed above the original list (#37767 (comment)) contains:
RazorViewAttribute
is marked as obsolete, so, I don't think we need to do anything (cc @mkArtakMSFT in case you want to have it added).Also,
ProvideRazorExtensionInitializerAttribute
is not part of the ASPNET.Core repo anymore.Usage Examples
Alternative Designs
Risks
Very small, this will introduce new types derived from the non-generic ones.
The text was updated successfully, but these errors were encountered: