本文介绍了继承自匿名类型T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,是否可以从匿名Type继承,我有一些类和接口,例如

Is it possible to inherits from an anonymous Type for example, I have some classes and interfaces like

public interface IFoo{
     int A { get; set;}
     string B { get; set;}
}

public interface IBoo : IFoo{
     decimal C { get; set;}
}

public class Foo: IFoo {...}

public class Boo: IBoo {...}

现在,我想通过添加一些以相同方式应用于所有类的新属性来定义扩展现有类的类:

now I want to define a class that extends the existing classes by adding some new properties which is applied to all class in the same way :

public class Extended<T> : T where T: IFoo
{
    bool D { get; set;}
}

因此,如果是 Extended< Foo> Extended< Foo> 内部的属性将是:

so as a result in case of Extended<Foo>, the properties inside the Extended<Foo> would be:

 public int A { get; set;}
 public string B { get; set;}
 public bool D { get; set;}

我知道这会导致编译错误:

I know this will give compilation error:

但是为什么这不适用?只要我在声明中加上约束条件

but why this is not applicable? as long as I am putting constraints in the declaration

当前解决方案:我应该创建一个实现 IFoo 的类 ExtendedFoo 和一个实现 IBoo ExtendedBoo ,我担心的是,是否有一种方法或变通办法来定义一个通用类,该通用类继承它们在实现接口Foo的所有这些类型的类.

The current solution: I should create a class ExtendedFoo which implements IFoo and ExtendedBoo which implements IBoo , my concern is if there is a way or workaround to be able to define one generic class that inherits all these type of classes where they implement the interface Foo.

希望我的问题很清楚

推荐答案

否,无法从C#中的泛型类型参数继承.实际上,也不可能从匿名类型继承,但这是一个不同的概念(请参阅"MS Docs匿名类型"条目).

No, it is not possible to inherit from generic type parameter in C#. It is also not possible to inherit from anonymous type as a matter of fact, but that is a distinct concept (see MS Docs Anonymous Types entry).

C#泛型的工作方式是 class Generic< T>{} 编译器会生成单个不完整的类型 Generic ,然后在运行时将其用于生成代码实现和类型(例如, Generic< string> Generic< int> ,...).参考类型的代码实现共享相同的生成代码: List< string> List< object> 是不同的类型,但是使用相同的代码.(对于相同大小的值类型也是如此,但是在这种情况下,因为我们在讨论继承,所以这是无关紧要的.)

C# generics work in a way where for class Generic<T> {} the compiler generates a single incomplete type Generic<> which is then used during runtime to generate code implementations and types (e.g. Generic<string>, Generic<int>, ...). The code implementations for reference types share the same generated code: List<string> and List<object> are distinct types but use same code. (The same also holds for value types of same size but that is irrelevant in this case as we are talking about inheritance.)

您的要求是具有通用类型 class Generic< T>:T .我可以想象有一种方法可以从中生成不完整的 Generic 类型.但是,每种类型的 T 可以具有不同的成员,虚方法表等.因此,将需要分别在每个子类中分别生成每个 Generic< T> 实例化的代码.类似于C ++模板的工作方式.

Your requirement is to have a generic type class Generic<T> : T. I could imagine there is a way of generating an incomplete type Generic<> from that. However, every type T can have distinct members, virtual method table, etc. Therefore a code for each Generic<T> instantiation would be required to be generated separately, in a manner similar to how C++ templates work.

并且这在C#编译器中目前是不可能的.造成这种情况的原因可能是没有人发现用例对设计语言解决方案,解决与现有功能的所有冲突并加以实施如此有价值.

And that is currently not possible with C# compiler. The reason for that is probably that no-one found the use-case so valuable to design the language solution, resolve all the conflicts with existing features, and implement it.

我建议您重新考虑您的设计.一旦开始混合泛型类型和功能(业务逻辑),您可能还是会逐渐摆脱惯用的C#.我希望进一步详细说明,但是从这个角度来看,您的问题似乎很像 xy问题.

I would recommend you to reconsider your design. As soon as you start mixing generic types and capabilities (business logic), you're probably moving away from idiomatic C# anyway. I would love to further elaborate on that, but your question seems very much like xy problem in that perspective.

这篇关于继承自匿名类型T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:15