简体   繁体   English

我将如何在功能上重新实现(或接近重新实现)C#中的泛型?

[英]How would I functionally re-implement (or close to re-implement) generics in C#?

I want to be able to write a series of classes in an inheritance hierarchy. 我希望能够在继承层次结构中编写一系列类。 Each class has to have a member Foo of a type T specific to that class. 每个类必须具有特定于该类的类型T的成员Foo。 In case it's relevant, no classes in this hierarchy will contain duplicate Foo classes. 如果相关,则此层次结构中的任何类都不会包含重复的Foo类。

My initial thought was to do this with generics. 我最初的想法是使用泛型。 For example, 例如,

public class ExampleBase<T> where T: GenericType
{
    public T Foo
    {
        get;
        set;
    }
}

public class ExampleChildOne<T> : ExampleBase<T> where T : GenericTypeTwo {}

public class ExampleChildLeaf : ExampleChildOne<GenericTypeLeaf> {}

public class GenericType {}

public class GenericTypeTwo : GenericType {}

public class GenericTypeLeaf : GenericTypeTwo {}

With this code, I am able to write things like var foo = new ExampleChildLeaf().Foo; 通过此代码,我可以编写var foo = new ExampleChildLeaf().Foo; The variable is the type I want, which is GenericTypeLeaf. 变量是我想要的类型,即GenericTypeLeaf。 This is good. 很好 However, I am unable to do something like ExampleBase example = new ExampleChildLeaf(); 但是,我无法做类似ExampleBase example = new ExampleChildLeaf(); because ExampleBase requires a generic type. 因为ExampleBase需要通用类型。 I also can't do ExampleBase<GenericType> example = new ExampleChildLeaf(); 我也不能做ExampleBase<GenericType> example = new ExampleChildLeaf(); because ExampleChildLeaf doesn't actually extend ExampleBase<GenericType> . 因为ExampleChildLeaf实际上并没有扩展ExampleBase<GenericType>

I've also tried a non-generic way to do this. 我还尝试了一种非通用的方法来执行此操作。 Each class had its own Foo which had the proper type (so one would have a GenericType Foo, one would have GenericTypeLeaf Foo, etc.). 每个类都有自己的Foo,它具有适当的类型(因此,一个类将具有GenericType Foo,一个将具有GenericTypeLeaf Foo,等等)。 The issue with this is that I can't override one Foo with a different one, since the types are different. 问题在于,由于类型不同,我无法用另一个Foo覆盖一个Foo。

So. 所以。 That all said, is there any way for me to functionally do something similar? 综上所述,我有什么办法在功能上做类似的事情吗? It's okay if it's a bit convoluted, because this will be in a library that should pretty much never me modified. 如果它有点令人费解,那是可以的,因为它将存在于一个几乎不应该修改的库中。 My goal is to make it as easy for the end user to use while abstracting away all of the generic arguments. 我的目标是使最终用户在提取所有通用参数的同时,也易于使用。

Also, I wasn't able to come up with a great way to phrase the title question. 另外,我无法提出一种很好的方式来表达标题问题。 I would appreciate suggestions for that as well. 我也将为此提出建议。 Thanks! 谢谢!

When you want ExampleBase example = new ExampleChildLeaf(); 当您需要ExampleBase example = new ExampleChildLeaf(); , this would possible by creating a non-generic base class that didn't know about Foo , but could still have the other functionality (assuming there is other functionality). ,这可以通过创建一个不了解Foo的非通用基类而实现,但仍可以具有其他功能(假设还有其他功能)。 That's pretty similar to how Task and Task<T> work. 这与TaskTask<T>工作方式非常相似。

public class ExampleBase
{
    //....
}

public class ExampleBase<T> : ExampleBase where T: GenericType
{
    public T Foo
    {
        get;
        set;
    }
}

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

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