简体   繁体   English

通用类型约束——我可以在方法级别上扩展类约束吗?

[英]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.

相关问题 类和方法级别泛型类型约束交互 - Class and Method level generic type constraints interaction 具有类型约束或基类参数的泛型方法 - Generic method with type constraints or base class parameter 我可以在泛型类型 class 中有不同的泛型类型方法吗 - Can I have a different generic type Method in a generic type class 通用类约束 - 我可以指定Generic类型可以序列化为XML吗? - Generic class constraints - can I specify that the Generic type can be serialized as XML? 我可以有多个 class 约束而不将约束设置为 C# 中的类型“类”吗 - Can I have multiple class constraints without setting the constraint to a type 'class' in C# 为什么我不能将一个泛型转换为另一个泛型? (类类型约束)C# - Why can't I cast a generic to another generic? (Class type constraints) C# 模拟具有类型约束的泛型方法 - Mocking a generic method with type constraints 如何定义具有两个通用类型和两个类型约束的方法? - How can I define a method with two Generic types and two type constraints? 通用方法类型不能用作通用类的通用类型 - Generic method type can't be used as generic type for generic class 具有接口作为类型约束的泛型类的c#扩展方法 - c# extension method for a generic class with interface as type constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM