什么很好的理由希望.NET泛型可以继承泛型参数类型之一?很好、理由、参数、类型

2023-09-02 20:47:30 作者:狂暴旳青春わ

这篇文章是在延续this 之一。

This post is in continuation of this one.

我想知道如果我是唯一一个谁错过,需要一个.NET泛型类型来继承其泛型参数类型之一的能力。

I am trying to understand if I am the only one who misses and needs the ability of a .NET generic type to inherit one of its generic parameter types.

目前的挑战是收集支持此功能的令人信服的理由,或者去知道有没有。

The challenge is to collect compelling reasons in favour of this feature or, alternatively, get to know that there are none.

我给我的理由,它作为这个问题的答案 - 见下文

I give my reason to have it as an answer to this question - see below.

我问乡亲们都来加入他们的回答这个帖子。

I am asking folks out there to add theirs as answers to this post.

如果您不同意该功能是非常有用的或有利于没有很好的理由 - 请从这里张贴任何东西不要,但你可以在开始它所有的原帖这样做 - here.

If you disagree that the feature is useful or have no good reasons in favour - please refrain from posting anything here, though you can do so in the original post that has started it all - here.

P.S。

某些C ++模式是不相关的.NET。例如,在他的优秀著作现代C ++设计安德烈Alexandrescu的介绍如何创建在编译评估类型的列表时间。当然,这种模式没有意义。NET,在那里,如果我需要的类型我刚刚创建列表℃的名单;类型> 和类型填充它。所以,让我们试着拿出原因相关的.NET框架,而不是盲目地翻译C ++编码技术为C#。

Some C++ patterns are irrelevant in .NET. For instance, in his excellent book Modern C++ Design Andrei Alexandrescu describes how to create a list of types evaluated at the compile time. Naturally, this pattern is irrelevant for .NET, where if I need a list of types I just create List<Type> and populate it with types. So, let us try to come up with reasons pertinent to the .NET framework and not just blindly translating C++ coding techniques to C#.

P.P.S。

当然,这种讨论是严格的学术。即使一百个令人信服的理由有问题的功能浮出水面它不会被执行,直到永远。

Of course, this discussion is strictly academic. Even if a hundred compelling reasons for the feature in question is surfaced it is not going to be implemented, ever.

推荐答案

时不时地,我偶然发现了一个执行问题,在这里我深感遗憾的是 C&LT; T&GT; 不能继承 T 。不幸的是,我从来没有记录了这些问题,所以我可以描述只是最近的一次 - 这也是我已经偶然发现了现在。所以在这里,它是:

Every now and then I stumble upon an implementation issue, where I deeply regret that C<T> cannot inherit T. Unfortunately, I have never recorded these issues and so I can describe just the most recent one - the one I have stumbled upon right now. So here it is:

我们的系统是可扩展的元数据通过,这在运行时变为可用。元数据被转换成在运行时动态生成的使用Reflection.Emit的一个类型。不幸的是,动态生成的类型必须满足以下条件:

Our system is extensible through metadata, which becomes available at run-time. The metadata is translated to a type dynamically generated at run-time using Reflection.Emit. Unfortunately, the dynamically generated type has to satisfy the following conditions:

在它必须来自一些其他类型的,作为参数传递给动态类型的创建者。这种类型称为的祖先类型并始终是一个静态编译类型。 必须实现多个接口,比如 IDynamicObject接口(我们的接口), System.ComponentModel.INotifyPropertyChanged Csla.Core.IMobileObject 。请注意,这种情况下放置一定的制约的祖先类型。例如,祖先类型可能不执行 IDynamicObject接口接口,如果所有的接口方法的抽象实现的除外。还有其他的限制,所有这些都必须进行检查。 应该(和它)覆盖对象方法等于的ToString GetHash code 。 It must derive from some other type, provided as a parameter to the dynamic type creator. This type is called the ancestor type and is always a statically compiled type. It must implement several interfaces, say IDynamicObject (our interface), System.ComponentModel.INotifyPropertyChanged and Csla.Core.IMobileObject. Note, that this condition places certain constraints on the ancestor type. For instance, the ancestor type may not implement the IDynamicObject interface, except if all the interface methods are implemented abstractly. There are other constraints, all of which must be checked. It should (and it does) override the object methods Equals, ToString and GetHashCode.

目前,我不得不使用Reflection.Emit的发射所有的IL code,以满足这些条件。当然,有些任务可能会被转发到静态编译code。例如,的Object.Equals(对象)方法调用的覆盖 DynamicObjectHelper(IDynamicObject接口,IDynamicObject接口)这是一个静态编译C#code做的工作的最大的散 相比较是否相等两个动态对象。但是,这更是一个例外,不是规则的 - 大多数实施发射,这是一个痛苦的对接

Currently, I had to use Reflection.Emit to emit all the IL code to satisfy these conditions. Of course, some tasks may be forwarded to statically compiled code. For instance, the override of the object.Equals(object) method invokes DynamicObjectHelper(IDynamicObject, IDynamicObject) which is a statically compiled C# code doing the largest bulk of the work of comparing two dynamic objects for equality. But this is more of an exception than the rule - most of the implementation is emitted, which is a pain in the butt.

那岂不是伟大的,是能够写一个泛型类型,如 DynamicObjectBase&LT; T&GT; 将被实例化的祖先类型,并作为实际的基本类型动态地生成的类型的?当我看到它时,泛型 DynamicObjectBase&LT; T&GT; 可以在静态编译的C#code实现大部分的动态类型的要求。动态发射的类型将继承它,也许,只是覆盖一些简单的虚方法。

Would not it be great to be able to write a generic type, like DynamicObjectBase<T> which will be instantiated with the ancestor type and serve as the actual base type of the dynamically generated type? As I view it, the generic type DynamicObjectBase<T> could implement much of the dynamic type requirements in a statically compiled C# code. The dynamically emitted type would inherit it and probably, just override a few simple virtual methods.

最后,我的令人信服的理由是,让 C&LT; T&GT; T 继承将大大简化发光动态类型的任务。

To conclude, my compelling reason is that letting C<T> inherit from T would greatly simplify the task of emitting dynamic types.