具有多个类约束的C#泛型类型

本文关键字:泛型类型 约束 | 更新日期: 2023-09-27 18:30:03

TL;DR

我想用C#编译

public void some_method< T >() where T : class1, class2

这可能吗?


完整上下文

除了一个参数外,我有两个相同的方法。

public SignInResponseMessage Generate(SignInRequestMessage request, (X509Certificate2 || WindowsPrincipal) principal, Uri requestUri)
{
    SignInResponseMessage response = null;
    ClaimsIdentity identity = null;
    if (principal != null)
    {
        identity = CreateSubject(principal);
        response = Generate(request, requestUri, identity);
    }
    else
    {
        throw new ArgumentNullException("principal");
    }
    return response;
}

我目前正在复制这种方法,这让我有点畏缩,因为我真的很想制作这个DRY-er。环顾四周,这个文档看起来很有希望,但它只允许我添加一个类约束。我在第二类上得到以下错误:

错误1类类型约束"class2"必须位于任何其他约束之前

如果WindowsPrincipalX509Certificate2是我编写的两个类,我可以很容易地让它们实现相同的接口,我会很乐意去,但这不是一个选项。

有什么方法可以完成我想做的事情吗

如果没有,我想了解更多关于使这不可能实现的潜在机制

具有多个类约束的C#泛型类型

我担心这可能会导致实际调用什么方法的问题。想象一下,如果指定的一个类从另一个类继承,并且有该方法的重写!?

有关原因的完整描述,请参阅"钻石问题"

如果你想解决这个问题。你可以用适配器设置通用共享接口,然后使用它。

interface IAdaper {
    SomeMethod();
}
class AdapterOne : IAdapter {
    TypeOneToCall  _one;
    public AdapterOne (TypeOneToCall one) {
        _one = one;
    }
    public SomeMethod() {
        return _one.SomeMethod();
    }
}
class AdapterTwo : IAdapter {
    TypeTwoToCall _two;
    public AdapterTwo (TypeTwoToCall two) {
        _two = two;
    }
    public SomeMethod() {
        return _two.SomeMethod();
    }
}
class Generic<T> where T : IAdapter {
    // Your implementation here.
}

如果将方法作为参数传递,则T可以是任何值:

  public SignInResponseMessage Generate<T>(SignInRequestMessage request, 
                                          Func<T, ClaimsIdentity> createSubject, 
                                          T principal, 
                                          Uri requestUri)
  {
      SignInResponseMessage response = null;
      ClaimsIdentity identity = null;
      if (principal != null)
      {
          identity = createSubject(principal);
          response = Generate(request, requestUri, identity);
      }
      else
      {
          throw new ArgumentNullException("principal");
      }
      return response;
  }

所以重用方法:

  var r1 = Generate<X509Certificate2>(request, CreateSubject, certificate, uri);
  var r2 = Generate<WindowsPrincipal>(request, CreateSubject, principal, uri);