简体   繁体   English

如何将Generic类作为参数传递给非泛型类构造函数

[英]How to pass a Generic class as a Parameter to a non-generic class constructor

Assume the following code (Please read my question in the code comments in the final class): 假设以下代码(请在最后一节的代码注释中阅读我的问题):

//This is my Generic Class
public class ClientRequestInfo<K, V>
{
    public string Id { get; set; }
    private Dictionary<K, V> parameters;

    public ClientRequestInfo()
    {
        parameters = new Dictionary<K, V>();
    }

    public void Add(K key, V value)
    {
        parameters.Add(key, value);
    }
 }

public class ProcessParameters()
{
    private void CreateRequestAlpha()
    {
        ClientRequestInfo<int, string> info = new ClientRequestInfo<int, string>();
        info.Add(1, "Hello");
        SynchRequest s = new SynchRequest(info);
        s.Execute();
    }
    private void CreateRequestBeta()
    {
        ClientRequestInfo<int, bool> info = new ClientRequestInfo<int, bool>();
        info.Add(1, true);
        SynchRequest s = new SynchRequest(info);
        s.Execute();
    }
}

public class SynchRequest
{
    //What type should I put here?
    //I could declare the class as SynchRequest<K, V> but I don't want
    //To make this class generic.
    private ClientRequestInfo<????,?????> info;
    private SynchRequest(ClientRequestInfo<?????,?????> requestInfo)
    {
        //Is this possible?
        this.info = requestInfo;
    }

    public void Execute()
    {}
}

If you don't want to make SynchRequestInfo generic, can you make a non-generic base class for ClientRequestInfo ? 如果您不想使SynchRequestInfo通用性,您是否可以为ClientRequestInfo创建一个非泛型基类? -- -

public abstract class ClientRequestInfo
{
    public abstract void NonGenericMethod();
}

public class ClientRequestInfo<K, V> : ClientRequestInfo
{
    public override void NonGenericMethod()
    {
        // generic-specific implementation
    }
}

Then: 然后:

public class SynchRequest
{
    private ClientRequestInfo info;

    private SynchRequest(ClientRequestInfo requestInfo)
    {
        this.info = requestInfo;
    }

    public void Execute()
    {
        // ADDED: for example
        info.NonGenericMethod();
    }
}

The class has to be generic if you want generic member variables. 如果您想要通用成员变量,那么该类必须是通用的。

private ClientRequestInfo<????,?????> info;

Is a generic member. 是通用会员。

You can use a non-generic base class or interface: 您可以使用非泛型基类或接口:

class ClientRequestInfo<K,Y> : IClientRequestInfo

But in your example, ClientRequestInfo doesn't have any non-generic members, so the interface/base class would be empty and have to be casted to the correct generic version to be useful anyway. 但在您的示例中,ClientRequestInfo没有任何非泛型成员,因此接口/基类将为空,并且必须转换为正确的通用版本才能使用。

You have two options, if you want to not make SynchRequestInfo generic. 如果您不想使SynchRequestInfo通用,您有两个选择。

  1. Make a non-generic base class, having both use it. 创建一个非泛型基类,同时使用它。
  2. Provide an interface to use, such as ISynchInfo, and have your generic class implement the interface. 提供要使用的接口,例如ISynchInfo,并让您的泛型类实现接口。

In this case, I'd prefer the interface approach, since you may want to synchronize other types (separate from client requests) in the future. 在这种情况下,我更喜欢接口方法,因为您可能希望将来同步其他类型(与客户端请求分开)。

使ClientRequestInfo(K,V)实现具有SynchRequest所需的接口函数的ClientRequestInfo接口。

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

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