[英]Generic type constraints - can I extend class constraint on method level?
I am working on some kind of a fluent API which would easily allow to register all necessary classes in the IoC container.我正在研究某种流畅的 API,它可以轻松地在 IoC 容器中注册所有必要的类。 For example:
例如:
builder
.WithInput<EncryptedMessage>()
.UseEncryptedMessageParser()
// ...
// .OtherEncryptedMessageFlowRegistrations()
builder
.WithInput<StandardMessage>()
.UseStandardMessageParser()
// ...
// .OtherStandardMessageFlowRegistrations()
What I'd like to achieve is to have less strict generic type constraints at the beginning, but make it more strict later in the hierarchy.我想要实现的是在开始时具有不太严格的泛型类型约束,但在层次结构的后期使其更加严格。 Therefore, I am wondering if it's somehow possible to create a class with generic type constraints and then extend this generic type with additional constraints on method level ?
因此,我想知道是否有可能以某种方式创建一个具有泛型类型约束的类,然后在方法级别上使用额外的约束来扩展这个泛型类型?
public class A<T> where T : class
{
public void Foo(T input) where T : SomeBaseClass // additional constraints for generic type on class level
{
}
}
More realistic example:更现实的例子:
public class MessageFlowBuilder<TInput> where TInput : class, new()
{
private readonly IServiceCollection _services;
public MessageFlowBuilder(IServiceCollection services)
{
_services = services;
}
public StandardMessageFlowBuilder<TInput> UseStandardMessageParser()
where TInput : StandardMessageBase // additional constraints for generic type on class level (required by parser)
{
_services.AddTransient<StandardMessageParser<TInput>>();
return new StandardMessageFlowBuilder<TInput>();
}
public EncryptedMessageFlowBuilder<TInput> UseEncryptedMessageParser()
where TInput : EncryptedMessageBase // additional constraints for generic type on class level
{
_services.AddTransient<EncryptedMessageParser<TInput>>();
return new EncryptedMessageFlowBuilder<TInput>();
}
}
Or maybe is it possible to create another generic type on method level which then would have a constraint to be of the same type as class level argument?或者是否有可能在方法级别上创建另一个泛型类型,然后将约束与类级别参数的类型相同?
public class A<T> where T : class
{
public void Foo<T2>(T2 input) where T2 : SomeBaseClass // + some T == T2 type equality?
{
}
}
Could could make Foo
a generic method, yes - but you can't constraint it to be the same as T
, and you can't add a constraint to an existing type parameter.可以使
Foo
成为通用方法,是的 - 但您不能将其约束为与T
相同,并且您不能向现有类型参数添加约束。
Could you try something like this?你能试试这样的东西吗? I am not even sure right now whether this will compile fine or not.
我现在甚至不确定这是否可以正常编译。 The method needs to be generic too on its own.
该方法本身也需要是通用的。
public class A<T> where T : class
{
public void Foo<U>(U input) where U : T, SomeBaseClass, SomeInterface //whatever
{
}
}
public class A<T> where T : class
{
public void Foo(T input) where T : SomeBaseClass // additional constraints for generic type on class level
{
}
}
No, the type T
is declared when you instantiate the class, you can't change that at method level.不,类型
T
是在实例化类时声明的,您不能在方法级别更改它。
So by doing:所以通过这样做:
new A<SomeBase>();
You have effectively given the method the signature:您已经有效地为该方法提供了签名:
public void Foo(SomeBase input)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.