Proposal Generic Attributes with constraints #569
Replies: 11 comments
-
To |
Beta Was this translation helpful? Give feedback.
-
Duplicated #124 |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h yes |
Beta Was this translation helpful? Give feedback.
-
@Logerfo Difference from #124 is |
Beta Was this translation helpful? Give feedback.
-
You describe |
Beta Was this translation helpful? Give feedback.
-
@HaloFour this is special case, TOwner is the type where this attribute is applied. Compiler will automatically substitute last TOwner as the same type where attribute is applied. |
Beta Was this translation helpful? Give feedback.
-
@neurospeech That just won't work. I'd be in favor of |
Beta Was this translation helpful? Give feedback.
-
@jnm2 skipping last generic parameter is only the case when attribute is applied, althought C# takes care of "Attribute" name as being optional, I think compiler can simply substitute class name as last parameter of generic argument list. I mean wrigint DIGlobal<TService,> , extra comma is ok, it does not hurt but it is kind of implicit here. Like how |
Beta Was this translation helpful? Give feedback.
-
@neurospeech The problem is that we need a convention to disambiguate between the two types |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Ok, I am fine with |
Beta Was this translation helpful? Give feedback.
-
@neurospeech The other benefit is that potentially, you could choose to do either |
Beta Was this translation helpful? Give feedback.
-
Most of the time in registering dependency via attribute has no compile time error for registering wrong types. Let us consider..
Lets assume that we are using Attribute
DIGlobal
to register dependencySMSService
for serviceIEmailService
.What if there is a generic type called
GenericAttribute<T>
, which allows us to use generics in attributes.TOwner
is the type on whichGenericAttribute<TOwner>
can be applied.Note,
TOwner
does not need to be written in DIGlobal because TOwner is the class on which the attribute is declared.TOwner
is the last type in generic parameters. AlsoTOwner
is not specified while using using attribute becauseTOwner
is the type on which attribute is applied. Here,DIGlobal<IEmailService,>
will substitute last generic attribute asDIGlobal<IEmailService,SMSService>
automatically.This isn't just limited to Dependency Injection but also in general registering attribute.
Beta Was this translation helpful? Give feedback.
All reactions