如何在列表中存储泛型类而不指定类型

本文关键字:类型 泛型类 存储 列表 | 更新日期: 2023-09-27 18:32:16

我有以下类:

class Parameter<T>
{
    public string Index { get; set; }
    public T Value { get; set; }
}

我想要一个这样的列表:

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

然后我将有如下代码:

...
Parameter<> parameter = parameterModel.Parameters.Where(p => p.Index == "AAA");
if (parameter is Parameter<bool>)
{
    ...
}
else if (parameter is Parameter<int>)
{
    ...
}
...

这是可行的还是我应该使用继承而不是泛型?

提前谢谢。

如何在列表中存储泛型类而不指定类型

不会

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

做这份工作吗?


或者,也许您不想使用不同类型的参数?然后你可以做

public interface IParameter
{
    string Index { get; set; }
    Type ParameterType { get; }
}
public class Parameter<T> : IParameter
{
    public string Index { get; set; }
    public T Value { get; set; }
    public Type ParameterType
    {
        get
        {
            return typeof(T);
        }
    }
}
class ParameterModel
{
    public List<IParameter> Parameters { get; }
}

所以你可以做到,

var aaaParameter = parameterModel.Parameters.Single(p => p.Index == "AAA");
if (aaaParameter.ParameterType == typeof(bool))
{
    ...
}
else if (aaaParameter.ParameterType == typeof(string))
{
    ...
}

但是,考虑到您所描述的问题的界限,一个简单的字典可能是最好的。

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

允许

var aaaParameter = parameterModel["AAA"];
if (aaaParameter is bool)
{
    ...
}
else if (aaaParameter is string)
{
    ...
}

你可以这样做 - 简短而甜蜜:

 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>();

您可以使用继承。

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

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") },
};