简体   繁体   English

c# - 无法从List转换 <DateTime?> 列表 <dynamic>

[英]c# - Cannot convert from List<DateTime?> to List<dynamic>

I am trying to create an attribute in a list that takes different types. 我试图在列表中创建一个属性不同的属性。 This is my class: 这是我的班级:

public class ChartData
{
    public string id { get; set; }
    public List<dynamic> data { get; set; }

    public ChartData()
    {
    }

    public ChartData(string id, List<DateTime?> data)
    {
        this.id = id;
        this.data = data;
    }
}

    public ChartData(string id, List<float?> data)
    {
        this.id = id;
        this.data = data;
    }

    public ChartData(string id, List<int?> data)
    {
        this.id = id;
        this.data = data;
    }

In the code I use the data list to store DateTime? 在代码中我使用data列表来存储DateTime? , float? float? or int? 还是int? data. 数据。 What do I do to be able to store these different types in one class attribute? 我该怎么做才能将这些不同的类型存储在一个类属性中?

I am getting the error: 我收到错误:

Argument 2: cannot convert from 'System.Collections.Generic.List<System.DateTime?>' to 'System.Collections.Generic.List<dynamic>'

I would recommend using Generics if you know the type prior to instantiation 如果您在实例化之前知道类型,我建议使用泛型

public class ChartData
{
   public string id { get; set; }
}

public class ChartData<T> : ChartData
{
    public List<T> data { get; set; }

    public ChartData()
    {
    }

    public ChartData(string id, List<T> data)
    {
        this.id = id;
        this.data = data;
    }
}

Usage: 用法:

ChartData<int> intData = new ChartData<int>("ID1", new List<int>());
ChartData<DateTime> dateData = new ChartData<DateTime>("ID1", new List<DateTime>());
ChartData<float> floatData = new ChartData<float>("ID1", new List<float>());



List<ChartData> list = new List<ChartData>() {
    intData,
    dateData,
    floatData
};

I think change below should work: FROM 我认为下面的改变应该有效: FROM

 public List<dynamic> data { get; set; }

TO

 public dynamic data { get; set; }

If it suits your case you can use covariant collection like this: 如果它适合您的情况,您可以使用这样的协变集合:

IReadOnlyList<dynamic> data;
data = new List<string>();

But it works with reference types only. 但它仅适用于引用类型。

If you don't care what type is it you can use List for your data. 如果您不关心它是什么类型,您可以使用List作为数据。

For more convenient access you can use something like this: 为了更方便的访问,您可以使用以下内容:

private IList data;
public IEnumerable<dynamic> Data { get { return data.OfType<dynamic>(); } }

Hope it helps. 希望能帮助到你。

You may use LINQ to convert list items' type: 您可以使用LINQ转换列表项的类型:

public ChartData(string id, List<DateTime?> data)
{
    this.id = id;
    this.data = data.Cast<dynamic>().ToList();
}

Important note: in this case you're creating a duplicate of the list and changes in the caller's instance of the list won't be reflected in the ChartData 's instance (which is probably even better, though.) 重要说明:在这种情况下,您正在创建列表的副本,并且调用者的列表实例中的更改将不会反映在ChartData的实例中(尽管可能更好)。

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

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