动态指定类型在C# [英] Dynamically specify the type in C#

查看:189
本文介绍了动态指定类型在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义DataSet,我受到一些限制:




  • 我想让用户指定类型

  • 我想要减少类型转换,因为我认为它会非常昂贵。

  • 我将使用数据在我的应用程序中非常频繁。



我不知道将在DataSet中存储什么类型的数据,所以我最初的想法是以使其成为对象 c>的列表 c>,但我怀疑频繁使用数据和需要类型



基本思想是:

  class DataSet:IDataSet 
{
私有字典< string,List< Object> _数据;

///< summary>
///构造给定用户指定标签的数据集。
///< / summary>
///< param name =labels>
///数据集中每列的标签。
///< / param>
public DataSet(List< string> labels)
{
_data = new Dictionary< string,List< object>
foreach(标签中的字符串标签)
{
_data.Add(label,new List< object>());
}
}

#region IDataSet成员

public List< string> DataLabels
{
get {return _data.Keys.ToList(); }
}

public int Count
{
get {_data [_data.Keys [0]]。 }
}

public List< object> GetValues(string label)
{
return _data [label];
}

public object GetValue(string label,int index)
{
return _data [label] [index]
}

public void InsertValue(string label,object value)
{
_data [label] .Insert(0,value);
}

public void AddValue(string label,object value)
{
_data [label] .Add(value);
}

#endregion
}

A将使用 DataSet 的具体示例是存储从 CSV 文件获得的数据,其中第一列包含标签。当从 CSV 文件加载数据时,我想指定类型而不是转换为 object 。数据可以包含诸如日期,数字,字符串等列。下面是它可能是什么样子:

 Date ,Song,Rating,AvgRating,User
02/03/2010,Code Monkey,4.6,4.1,joe
05/27/2009 ,Code Monkey,1.2,4.5,jill

机器学习/人工智能算法,因此我必须使数据的读取非常快。我想尽可能地消除类型转换,因为我无法从对象转换为每次读取所需的任何数据类型。



我已经看到的应用程序,允许用户选择csv文件中的每个项目的特定数据类型,所以我试图做一个类似的解决方案,可以为每个列指定不同的类型。我想创建一个通用的解决方案,所以我不必返回 List< object> ,但 List< DateTime> (如果它是一个DateTime列)或 List< double> (如果它是一个双精度的列)。



有什么办法可以实现吗?也许我的做法是错误的,是否有更好的方法来解决这个问题?

解决方案

现在。也许性能会很好。



您还可以将每个字段存储为一个变体对象,如下所示:

  struct变体
{
string StringValue;
DateTime DateTimeValue;
bool BoolValue;
// ...等...
}

只需要从结构体访问适当的成员,但这可能添加与内存使用和if语句...的开销...


I'm creating a custom DataSet and I'm under some constrains:

  • I want the user to specify the type of the data which they want to store.
  • I want to reduce type-casting because I think it will be VERY expensive.
  • I will use the data VERY frequently in my application.

I don't know what type of data will be stored in the DataSet, so my initial idea was to make it a List of objects, but I suspect that the frequent use of the data and the need to type-cast will be very expensive.

The basic idea is this:

class DataSet : IDataSet
{
    private Dictionary<string, List<Object>> _data;

    /// <summary>
    /// Constructs the data set given the user-specified labels.
    /// </summary>
    /// <param name="labels">
    /// The labels of each column in the data set.
    /// </param>
    public DataSet(List<string> labels)
    {
        _data = new Dictionary<string, List<object>>();
        foreach (string label in labels)
        {
            _data.Add(label, new List<object>());
        }
    }

    #region IDataSet Members

    public List<string> DataLabels
    {
        get { return _data.Keys.ToList(); }
    }

    public int Count
    {
        get { _data[_data.Keys[0]].Count; }
    }

    public List<object> GetValues(string label)
    {
        return _data[label];
    }

    public object GetValue(string label, int index)
    {
        return _data[label][index];
    }

    public void InsertValue(string label, object value)
    {
        _data[label].Insert(0, value);
    }

    public void AddValue(string label, object value)
    {
        _data[label].Add(value);
    }

    #endregion
}

A concrete example where the DataSet will be used is to store data obtained from a CSV file where the first column contains the labels. When the data is being loaded from the CSV file I'd like to specify the type rather than casting to object. The data could contain columns such as dates, numbers, strings, etc. Here is what it could look like:

"Date","Song","Rating","AvgRating","User"
"02/03/2010","Code Monkey",4.6,4.1,"joe"
"05/27/2009","Code Monkey",1.2,4.5,"jill"

The data will be used in a Machine Learning/Artificial Intelligence algorithm, so it is essential that I make the reading of data very fast. I want to eliminate type-casting as much as possible, since I can't afford to cast from 'object' to whatever data type is needed on every read.

I've seen applications that allow the user to pick the specific data type for each item in the csv file, so I'm trying to make a similar solution where a different type can be specified for each column. I want to create a generic solution so I don't have to return a List<object> but a List<DateTime> (if it's a DateTime column) or List<double> (if it's a column of doubles).

Is there any way that this can be achieved? Perhaps my approach is wrong, is there a better approach to this problem?

解决方案

I would suggest trying what you have now. Maybe the performance will be good enough. If not, and only then, you could think about optimizing further.

You could also store each field as a variant object like this:

struct Variant
{
   string StringValue;
   DateTime DateTimeValue;
   bool BoolValue;
   // ... etc. ...
}

Then you would just need to access the appropriate member from the struct, but this may add just as much overhead with the memory usage and if statements...

这篇关于动态指定类型在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆