为什么我需要重新声明在泛型类型的子类约束 [英] Why Do I need to redeclare type constraint in generic subclass

查看:258
本文介绍了为什么我需要重新声明在泛型类型的子类约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我试图创建通过实现一个通用的接口,通用的子类。

Recently I tried to create a generic subclass by implementing a generic interface.

public interface IModule<T> where T : DataBean { ..... }
public class Module<T> : IModule<T> where T : DataBean { .... } 



看来我不能依赖于任何T的限制的情况基本接口,
被定义,我需要重新声明它们自己。

It seems I can't rely on any of T's restrictions as were defined in the base interface, and I need to re-declare them myself.

MSDN 的只是提供:

在使用该子类的泛型类型
参数,你必须重复的
限制在规定的子级基地
级水平。对于
为例,推导约束

When using the subclass generic type parameters, you must repeat any constraints stipulated at the base class level at the subclass level. For example, derivation constraint

为什么是不是可以推断从基类/接口的限制?

Why isn't it possible to infer the constraints from the base class/interface ?

推荐答案

我无法想出任何原因,在C#无法理论上超过限制复制。但是,让我们复制(或增加),其中明确似乎是可用性的最简单的方法的记录的行为。

I can't come up with any reason that the c# couldn't theoretically copy over constraints. But the documented behavior of making us copy (or augment) them explicitly seems to be the simplest way for usability.

public class A{}
public class B : A {}

public class X<T> where T: A {}
public class Y<T> : X<T> where T: B { }

在上面,注意,我没得explicty拷贝过来的T> ,因为 B 总是的一个<$ C $关于 Y'LT约束C> A

In the above, note that I did not have to explicty copy over the constraint on Y<T>, because a B is always an A.

现在让我们看一下,如果编译过的约束会自动复制会发生什么。比方说,我定义 Y'LT; T> 无约束和编译器会自动把它们。在大量的代码;我用 Y'LT; T>。然后我改变对的声明的约束X'LT; T> 来包括一些新的接口。

Now lets look at what happens if the "compiler automatically copies over constraints". Let's say I define Y<T> without constraints and the compiler places them automatically. I use Y<T> in lots of code. Then I change the constraints on the declaration of X<T> to include some new interface.

改变 X<的声明中的编译器错误; T> 是在那里我的网站使用 Y'LT; T> <!/ code>

The compiler errors for changing the declaration of X<T> are at the sites where I use Y<T> !

使用C#编译器目前的工作方式,编译器错误在惯例X'LT; T> ,就好像我在一个破的方式改变它,我期望的那样。

With the way the c# compiler currently works, the compiler errors are at the usages of X<T>, as I would expect if I change it in a breaking way.

因此,尽管它会在某些情况下是方便的,这也将是其他人有点混乱。虽然两者都是有效的方法,我会假设(请注意,我不能够读取C#设计团队的头脑),这是一个主观判断,而不是一个单纯的技术工作。

So while it would be convenient in some scenarios, it would also be somewhat confusing in others. While both are valid approaches, I would assume (note that I'm not able to read the minds of the c# design team) that it was a judgement call and not a purely technical one.

我说:不是单纯的技术,但我肯定可以想像一些接口场景中会有些简单验证所有约束条件都能满足,而不是生产简单约束满足所有需要继承约束。

I say "not purely technical", but I can certainly imagine some interface scenarios where it would be somewhat simpler to verify that all constraints are met, rather than produce the simplest constraint that meets all required inherited constraints.

这篇关于为什么我需要重新声明在泛型类型的子类约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆