简体   繁体   English

使用泛型类作为非泛型类C#的属性

[英]Using generic class as property of non-generic class C#

Say I have the following two classes, which ideally do what I want: 说我有以下两个课程,理想情况下,它们可以满足我的要求:

Foo.cs: Foo.cs:

public class Foo
{
    private int Type { get; set; } = 0;

    public FooHelper FooHelper
    {
        get
        {
            switch (Type)
            {
                case 1:
                    return new FooHelper<Guid>("Foo", "Bar", "foobar", "", Guid.NewGuid());
                case 2:
                default:
                    return new FooHelper<int>("Bar", "Foo", "", "", 11);
            }
        }
    }

    public Foo(int type)
    {
        Type = type;
    }
}

FooHelper.cs: FooHelper.cs:

public class FooHelper<T> where T : struct
{
    public string Controller { get; set; }    = string.Empty;
    public string Action { get; set; }        = string.Empty;
    public string Area { get; set; }          = string.Empty;
    public string NameParameter { get; set; } = string.Empty;
    public T IdParameter { get; set; }        = default(T);

    public FooHelper() { }
    public FooHelper(string controller, string action, string area, string name, T id)
    {
        Controller    = controller;
        Action        = action;
        Area          = area;
        NameParameter = name;
        IdParameter   = id;
    }
}

Depending on the integer value passed in as parameter for the constructor of non-generic class Foo , the property FooHelper returns a new instance of the generic class FooHelper based on either a Guid or an int. 根据作为非泛型类Foo的构造函数的参数传入的整数值,属性FooHelper返回基于Guid或int的泛型类FooHelper的新实例。

However, I cannot seem to make this work without making Foo also generic, which I don't want. 但是,如果不使Foo也具有通用性,我似乎无法使这项工作有效,这是我所不希望的。 The error I'm getting is on the property line of FooHelper, stating: 我得到的错误是在FooHelper的属性行上,指出:

Using the generic type 'FooHelper<T>' requires 1 type arguments

which makes sense, but which I don't know at that point. 这是有道理的,但那一点我还不知道。 I want to determine the type of FooHelper inside the constructor of Foo . 我想确定Foo的构造函数内的FooHelper的类型。

Am I missing something or is it just not possible to do what I want in this case? 我是否缺少某些东西,或者在这种情况下是否无法做我想做的事情?

The problem is that public FooHelper FooHelper won´t even compile because there is no type called FooHelper . 问题在于,公共FooHelper FooHelper甚至不会编译,因为没有名为FooHelper类型。 However there are FooHelper<int> and FooHelper<Guid> which however don´t have anything in common. 但是,有FooHelper<int>FooHelper<Guid> ,但是它们没有任何共同之处。 Thus this porblem is (again) caused by the fact that you supply an information (the type-switch) at runtime and expect the compiler to infer the right generic type at compile-time, which isn´t possible. 因此,这种麻烦(再次)是由于您在运行时提供了一个信息(类型切换),并期望编译器在编译时推断正确的泛型而造成的。

What you can do instead is create a non-generic interface from that all your helpers derive and which your method returns: 相反,您可以做的是从所有助手派生的方法中创建一个非泛型接口,然后该方法返回:

public interface IFooHelper { ... }
public class FooHelper<T> : IFooHelper where T : struct { ... }

Now within your Foo -class: 现在在您的Foo类中:

public IFooHelper FooHelper
{
    get
    {
        switch (Type)
        {
            case 1:
                return new FooHelper<Guid>("Foo", "Bar", "foobar", "", Guid.NewGuid());
            case 2:
            default:
                return new FooHelper<int>("Bar", "Foo", "", "", 11);
        }
    }
}

However as mentioned above during compile-time there is no way to know what the actual generic type-parameter actually is, because that type doesn´t even exist. 但是,如上所述,在编译时无法知道实际的通用类型参数实际上是什么,因为该类型甚至不存在。

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

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