gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:25:02 24 4
gpt4 key购买 nike

<分区>

假设以下代码(请在最后一节课的代码注释中阅读我的问题):

//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()
{}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com