简体   繁体   English

实现缺少新约束的通用接口

[英]Implement a generic interface missing new constraint

Consider the following interface: 请考虑以下界面:

public interface IFoo
{
    M Bar<M>();
}

Trying to implement that with 试着用它来实现

class Foo : IFoo
{
    public M Bar<M>()
    {
        return new M();
    }
}

does not work, the compiler complains the M is missing a new() constraint. 不起作用,编译器抱怨M缺少new()约束。

When I add the constraint as in 当我添加约束时

class Foo : IFoo
{
    public M Bar<M>() where M : new()
    {
        return new M();
    }
}

this still does not do the trick, as the constraints of Foo.Bar do not match the constraints of the interface method now (and I'm not able to change that). 这仍然不能解决问题,因为Foo.Bar的约束现在与接口方法的约束不匹配(而且我无法改变它)。

The documentation for the compiler error CS0425 says 编译器错误CS0425文档

To avoid this error, make sure the where clause is identical in both declarations, or implement the interface explicitly. 要避免此错误,请确保两个声明中的where子句相同,或明确实现接口。

If "implementing the interface explicitly" is the solution: How do I do it? 如果“明确地实现接口”是解决方案:我该怎么做?

If you can't change the interface definition, you'll have to avoid using new M(); 如果您无法更改接口定义,则必须避免使用new M(); - use Activator.CreateInstance instead: - 改用Activator.CreateInstance

class Foo : IFoo
{
    public M Bar<M>()
    {
        return Activator.CreateInstance<M>();
    }
}

Of course, you may now run into a runtime error if there's no parameterless constructor for M , but that's unavoidable (again, because we can't change the generic constraints). 当然,如果M没有无参数构造函数,您现在可能会遇到运行时错误,但这是不可避免的(同样,因为我们无法更改泛型约束)。


Re: documentation: 回复:文档:

implement the interface explicitly. 明确地实现接口。

I think that what they're trying to get at here is "if you've got a base class method that has one set of generic constraints and you want to implement an interface that has a different set of constraints for a method with the same name, explicit implementation is one way out of that bind". 认为他们试图在这里得到的是“如果你有一个基类方法,它有一组通用约束,你想要实现一个具有不同约束条件的接口,对于一个具有相同方法的方法name,显式实现是该绑定的一种方法“。

Implementing the interface explicitly is not the solution. 明确地实现接口不是解决方案。 The compiler is simply telling you that if you need a generic method Bar with that constraint, then implement the interface explicitly so both versions of Bar can coexist, but that is, obviously, not the solution your are lookig for. 编译器只是告诉你,如果你需要一个具有该约束的泛型方法Bar ,那么显式地实现接口,这样两个版本的Bar可以共存,但显然,这不是你所期待的解决方案。

The only solutions are: 唯一的解决方案是:

  1. Implement the generic type constraint in the interface. 在接口中实现泛型类型约束。
  2. Instantiate the new M via reflection: Activator.CreateInstance and pay the price of losing type safety at compile time; 通过反射实例化新的MActivator.CreateInstance并在编译时支付丢失类型安全的代价; nothing enforces M to have a parameterless constructor. 没有强制M有一个无参数的构造函数。

You can generate explicit interface implementation by right clicking interface in class which should implement it and selecting "implement explicitly". 您可以通过右键单击类中的接口来生成显式接口实现,该接口应该实现它并选择“明确实现”。 Methods names should be pereceded with interface's name. 方法名称应该用接口名称来表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM