Let us use static classes as type parameters #5517
Replies: 4 comments 5 replies
-
Now that with static interface methods, static class with static-only interface is a thing now. The generic type must declare it supports static class so that it can't create any instance of |
Beta Was this translation helpful? Give feedback.
-
What is the use case for this? What is a generic type which can receive |
Beta Was this translation helpful? Give feedback.
-
Extremely similar to #4840. |
Beta Was this translation helpful? Give feedback.
-
Using static classes as generic type arguments is included under option 2 in Stephen Toub's proposal at #5783. |
Beta Was this translation helpful? Give feedback.
-
As an example, many of the
Microsoft.Extensions
library services take a type that is uses as a sort of tag or category rather than a type that should be instantiated (seeIStringLocalizer<>
,ILogger<>
, etc).Now in our code base we also have a few static classes that contain identifiers like so:
It is impossible for us to now provide translations for these identifiers by creating an instance of
IStringLocalizer<TechniqueId>
, since static classes can't be used as type parameters.We hacked around this by making the class uninstantiatable, but it obviously isn't a great solution:
We would prefer if C# was less restricting in this case and allowed us to pass a static class type to
IStringLocalizer<>
.Beta Was this translation helpful? Give feedback.
All reactions