C# 如何在不指定类型的情况下在列表中存储泛型类?

C# 如何在不指定类型的情况下在列表中存储泛型类?,c#,.net,list,generics,C#,.net,List,Generics,我有以下课程: class Parameter<T> { public string Index { get; set; } public T Value { get; set; } } 类参数 { 公共字符串索引{get;set;} 公共T值{get;set;} } 我想要一份这样的清单: class ParameterModel { public List<Parameter<>> Parameters {get;} } 类参

我有以下课程:

class Parameter<T>
{
    public string Index { get; set; }
    public T Value { get; set; }
}
类参数
{
公共字符串索引{get;set;}
公共T值{get;set;}
}
我想要一份这样的清单:

class ParameterModel
{
    public List<Parameter<>> Parameters {get;}
}
类参数模型
{
公共列表参数{get;}
}
然后我将有如下代码:

...
Parameter<> parameter = parameterModel.Parameters.Where(p => p.Index == "AAA");
if (parameter is Parameter<bool>)
{
    ...
}
else if (parameter is Parameter<int>)
{
    ...
}
...
。。。
Parameter Parameter=parameterModel.Parameters.Where(p=>p.Index==“AAA”);
if(参数为参数)
{
...
}
else if(参数为参数)
{
...
}
...
这是可行的还是应该使用继承而不是泛型

提前谢谢。

不会的

class ParameterModel<T>
{
    public List<Parameter<T>> Parameters { get; }
}

但是,考虑到你描述的问题的范围,一本简单的字典可能是最好的

var parameterModel = new Dictionary<string, object>();

你可以这样做-短而甜:

 public class Parameter
 {
     public string Index { get; set; }
     public dynamic Value { get; set; }
 }

 var param1 = new Parameter() { Index = "qw", Value = 34 };
 var param2 = new Parameter() { Index = "qr", Value = "rere" };

 int val1 = (int)param1.Value;
 string val2 = (string)param2.Value;
要获取类型,请执行以下操作:

  var t1 = param1.Value.GetType();
  var t2 = param2.Value.GetType();
或者在你的情况下是这样的:

 if (param1.Value.GetType() == typeof(int))
 {...}
 else if (param1.Value.GetType() == typeof(string))
 {...}
带着

 List<Parameter> myParams=new List<Parameter>();
List myParams=new List();

您可以使用继承。

以下是最近一些工作的一个例子:

abstract public class AttributeVariant
{
    string key;
    string name;

    public AttributeVariant(string key, string name)
    {
        this.key = key;
        this.name = name;
    }

    public string GetKey()
    {
        return key;
    }

    public string GetName()
    {
        return name;
    }
}

public class AttributeVariant<T> : AttributeVariant
{
    T value;

    public AttributeVariant(string key, string name, T value = default) : base(key, name)
    {
        this.value = value;
    }

    public object GetValue()
    {
        return value;
    }

    public void SetValue(T value)
    {
        this.value = value;
    }
}

public class AttributeVariant<T, T2> : AttributeVariant<T>
{
    T2 data;

    public AttributeVariant(string key, string name, T value = default, T2 data = default) : base(key, name, value)
    {
        this.data = data;
    }

    public object GetData()
    {
        return data;
    }

    public void SetData(T2 data)
    {
        this.data = data;
    }
}
抽象公共类AttributeVariant
{
字符串键;
字符串名;
公共属性变量(字符串键、字符串名称)
{
this.key=key;
this.name=名称;
}
公共字符串GetKey()
{
返回键;
}
公共字符串GetName()
{
返回名称;
}
}
公共类AttributeVariant:AttributeVariant
{
T值;
公共属性变量(字符串键,字符串名称,T值=默认值):基(键,名称)
{
这个值=值;
}
公共对象GetValue()
{
返回值;
}
公共无效设置值(T值)
{
这个值=值;
}
}
公共类AttributeVariant:AttributeVariant
{
T2数据;
公共属性变量(字符串键、字符串名称、T值=默认值、T2数据=默认值):基(键、名称、值)
{
这个数据=数据;
}
公共对象GetData()
{
返回数据;
}
公共无效设置数据(T2数据)
{
这个数据=数据;
}
}
通过这种方式,我可以将泛型类类型存储到一个简单的变量中,而不会出现任何问题。以下是我如何使用这些:

static Dictionary<string, AttributeVariant> AttributesList = new Dictionary<string, AttributeVariant>()
{
    { "age", new AttributeVariant<string>("age", "Age") },
    { "arms", new AttributeVariant<string>("arms", "Arms") },
    { "bridge", new AttributeVariant<string>("bridge", "Bridge") },
    { "color", new AttributeVariant<string>("color", "Model") },
    { "condition", new AttributeVariant<string, double>("condition", "Condition") },
    { "full_color", new AttributeVariant<string>("full_color", "Full Color") },
    { "gender", new AttributeVariant<string>("gender", "Gender") },
    { "height", new AttributeVariant<string>("height", "Height") },
    { "lenses", new AttributeVariant<string>("lenses", "Lenses") },
    { "material", new AttributeVariant<string>("material", "Material") },
    { "rim_type", new AttributeVariant<string>("rim_type", "Rim Type") },
    { "shape", new AttributeVariant<string>("shape", "Shape") },
};
static Dictionary AttributesList=new Dictionary()
{
{“年龄”,新属性变量(“年龄”,“年龄”)},
{“arms”,新属性变量(“arms”,“arms”)},
{“bridge”,新属性变量(“bridge”,“bridge”)},
{“颜色”,新属性变量(“颜色”,“模型”)},
{“条件”,新属性变量(“条件”,“条件”)},
{“全彩”,新属性变量(“全彩”,“全彩”)},
{“性别”,新的属性变量(“性别”,“性别”)},
{“height”,新属性变量(“height”,“height”)},
{“透镜”,新的属性变量(“透镜”,“透镜”)},
{“材料”,新属性变量(“材料”,“材料”)},
{“rim_类型”,新属性变量(“rim_类型”,“rim类型”)},
{“shape”,新属性变量(“shape”,“shape”)},
};

在这种情况下,您需要使用继承并执行动态分派。为什么不使用
ParameterModel
?@SriramSakthivel:请检查Jodrell响应。我只需要一个参数模型,因为无论支持值类型如何,我都需要为参数列表建模。当我和他们一起工作时,我会检查后面的类型。第二种解决方案看起来更好,我会给它一个机会。你不能切换类型。需要使用if-else链或切换
TypeCode
@SoMoS,最后一个选项直接指向我。@Jodrell这更糟糕
is
关键字不能与type一起使用。它需要一个静态
类型(类名)
。例如:
obj是String
,您如何迭代AttributesList字典并为每个条目打印“value”字段(及其类型)?即使使用反射,您也不知道前面的类型。更具体地说,如何将“value”字段强制转换为迭代中的变量。您可以使用.GetType().GetGenericArguments()检索泛型参数的类型,并相应地调整代码的行为。当然,您必须事先知道如何处理这些类型。如果我想知道我有一个双泛型参数还是单泛型参数,我可以计算GetGenericArguments()返回的数组的长度,也可以更具体一些,比如.GetType().GetGenericTypeDefinition()==typeof(AttributeVariant).GetType().GetGenericTypeDefinition()==typeof(AttributeVariant)
 List<Parameter> myParams=new List<Parameter>();
abstract public class AttributeVariant
{
    string key;
    string name;

    public AttributeVariant(string key, string name)
    {
        this.key = key;
        this.name = name;
    }

    public string GetKey()
    {
        return key;
    }

    public string GetName()
    {
        return name;
    }
}

public class AttributeVariant<T> : AttributeVariant
{
    T value;

    public AttributeVariant(string key, string name, T value = default) : base(key, name)
    {
        this.value = value;
    }

    public object GetValue()
    {
        return value;
    }

    public void SetValue(T value)
    {
        this.value = value;
    }
}

public class AttributeVariant<T, T2> : AttributeVariant<T>
{
    T2 data;

    public AttributeVariant(string key, string name, T value = default, T2 data = default) : base(key, name, value)
    {
        this.data = data;
    }

    public object GetData()
    {
        return data;
    }

    public void SetData(T2 data)
    {
        this.data = data;
    }
}
static Dictionary<string, AttributeVariant> AttributesList = new Dictionary<string, AttributeVariant>()
{
    { "age", new AttributeVariant<string>("age", "Age") },
    { "arms", new AttributeVariant<string>("arms", "Arms") },
    { "bridge", new AttributeVariant<string>("bridge", "Bridge") },
    { "color", new AttributeVariant<string>("color", "Model") },
    { "condition", new AttributeVariant<string, double>("condition", "Condition") },
    { "full_color", new AttributeVariant<string>("full_color", "Full Color") },
    { "gender", new AttributeVariant<string>("gender", "Gender") },
    { "height", new AttributeVariant<string>("height", "Height") },
    { "lenses", new AttributeVariant<string>("lenses", "Lenses") },
    { "material", new AttributeVariant<string>("material", "Material") },
    { "rim_type", new AttributeVariant<string>("rim_type", "Rim Type") },
    { "shape", new AttributeVariant<string>("shape", "Shape") },
};