C# 如何创建未知值类型的字典对象列表?
我正在创建一个包含各种类型信息的C# 如何创建未知值类型的字典对象列表?,c#,generics,design-patterns,C#,Generics,Design Patterns,我正在创建一个包含各种类型信息的CInformation类。它将公开的一类信息是参数。每个参数都可以使用以下任意类型键入:int、short、string。此外,基于字符串键,任何参数都可能有多个可能的值。所以我想创建一个字典来保存一个参数的所有可能值,但是当我试图声明我的参数列表时,问题就出现了。我创建了以下类: public class CParameter<T> { public object ParameterType { get; set; } publ
CInformation
类。它将公开的一类信息是参数
。每个参数都可以使用以下任意类型键入:int
、short
、string
。此外,基于字符串
键,任何参数
都可能有多个可能的值。所以我想创建一个字典
来保存一个参数的所有可能值,但是当我试图声明我的参数
列表时,问题就出现了。我创建了以下类:
public class CParameter<T>
{
public object ParameterType { get; set; }
public Dictionary<string,T> ValueByString;
}
public class CInformation
{
public string Version { get; set; }
public string Name{ get; set; }
public List<CParameter<object>> Parameters; // cannot cast any of my types to object, obviously!
}
公共类参数
{
公共对象参数类型{get;set;}
公共字典ValueByString;
}
公共类信息
{
公共字符串版本{get;set;}
公共字符串名称{get;set;}
公共列表参数;//显然无法将我的任何类型强制转换为对象!
}
有什么建议可以让我回避我的问题吗?我对我的问题有不同的解决方案,不一定是我上面的设计。多谢各位
编辑:我想实现的主要功能是能够拥有不同值类型的词典列表。使用
对象
专门化泛型类型是相当可疑的。如果这样做,您甚至可能根本不使用泛型类型。:-)
我认为这里的问题是,您希望CParameter
的实例专门用于不同的参数类型,并且希望CInformation
类上的参数列表包含不同类型的CParameter
换言之:
namespace Scratch
{
class Program
{
static void Main(string[] args)
{
CParameter<int> ints = new CParameter<int>();
CParameter<long> longs = new CParameter<long>();
CInformation info = new CInformation();
info.AddParameter(ints);
info.AddParameter(longs);
CParameter<int> ints2 = info.GetParameter<int>();
// ints2 and ints will both refer to the same CParameter instance.
}
}
public class CParameter<T>
{
public Type ParameterType { get { return typeof(T); } }
public Dictionary<string, T> ValueByString;
}
public class CInformation
{
public string Version { get; set; }
public string Name { get; set; }
private List<object> parameters;
public CInformation()
{
this.parameters = new List<object>();
}
public void AddParameter<T>(CParameter<T> parameter)
{
this.parameters.Add(parameter);
}
public CParameter<T> GetParameter<T>()
{
foreach (object parameter in this.parameters)
{
if (parameter is CParameter<T>)
return (CParameter<T>)parameter;
}
throw new Exception("Parameter type " + typeof(T).FullName + " not found.");
}
}
}
namespace Scratch
{
班级计划
{
静态void Main(字符串[]参数)
{
CParameter ints=新的CParameter();
CParameter longs=新的CParameter();
CInformation info=新的CInformation();
info.AddParameter(ints);
info.AddParameter(longs);
CParameter ints2=info.GetParameter();
//ints2和ints都将引用同一个CPParameter实例。
}
}
公共类参数
{
公共类型参数类型{get{return typeof(T);}
公共字典ValueByString;
}
公共类信息
{
公共字符串版本{get;set;}
公共字符串名称{get;set;}
私有列表参数;
公共信息()
{
this.parameters=新列表();
}
public void AddParameter(cpParameter参数)
{
this.parameters.Add(参数);
}
公共CParameter GetParameter()
{
foreach(此.parameters中的对象参数)
{
if(参数为CParameter)
返回(CParameter)参数;
}
抛出新异常(“参数类型”+typeof(T).FullName+“未找到”);
}
}
}
请注意,该示例中的列表
也可以是数组列表
另外,我有一种预感,您将希望按名称而不仅仅是按类型检索那些CParameter
对象。因此,考虑将name属性添加到或者更好的方法是,将参数存储在
字典中,而不是仅仅存储在列表中,并将参数名称用作键。您能否将信息
也设置为通用的,并将列表参数设置为公共的代码>。我不确定我是否理解您的问题,您可能还想将CParameter.ParameterType
从object
更改为T
。这不是真的,因为Parameters
列表应该有多个CParameter对象,每个对象的类型都不同。编辑:是的,关于ParameterType
你是对的。那么,最明显的问题是你想发送什么消息?您希望它们都实现哪些角色(接口)?如果没有-您可以创建泛型类型并将其隐式转换为对象。如果您想通过某种协议与所有这些对象交互,只需声明列表参数代码>。您需要重新考虑泛型是否是一种好方法。同样,您需要更详细地解释您的问题域,以便我们能够给您一个更具体的答案。我强烈建议您在使用泛型之前至少要大致理解一个链接。