Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中解析对象数组#_C#_Oop_Parsing_Data Structures_Tibco - Fatal编程技术网

C# 在C语言中解析对象数组#

C# 在C语言中解析对象数组#,c#,oop,parsing,data-structures,tibco,C#,Oop,Parsing,Data Structures,Tibco,我有一个对象数组,我想将它传递到一个只接受DOUBLE、NULL、STRING或DATETIME的方法中。因此,当我尝试传入该值时,它会给出一个错误,说明我无法传入任何任意对象,并且必须首先将其解析为DOUBLE、NULL、STRING或DATETIME foreach (var currRow in dataSet.Tables[0].Rows) { var tuple = Com.Tibco.As.Space.Tuple.Create(); //here is where

我有一个对象数组,我想将它传递到一个只接受
DOUBLE
NULL
STRING
DATETIME
的方法中。因此,当我尝试传入该值时,它会给出一个错误,说明我无法传入任何任意对象,并且必须首先将其解析为
DOUBLE
NULL
STRING
DATETIME

foreach (var currRow in dataSet.Tables[0].Rows)
{
    var tuple = Com.Tibco.As.Space.Tuple.Create();

    //here is where i loop through the object array
    for (int i = 0; i < currRow.Values.Length; i++)
    {
        //here is where i try to pass it to the method (which doesn't accept it)
        tuple.Put(dataSet.Tables[0].ColumnNames[i], currRow.Values[i]);
    }

    inSpace_.Put(tuple);
}
foreach(dataSet.Tables[0].Rows中的var currRow)
{
var tuple=Com.Tibco.As.Space.tuple.Create();
//这里是我循环遍历对象数组的地方
对于(int i=0;i

简而言之,我需要一种方法来解析每个对象并将其转换为适当的对象,然后将其放入元组中

编辑

以下是我试图做的,但没有成功:

foreach (var currRow in dataSet.Tables[0].Rows)
{
    var tuple = Com.Tibco.As.Space.Tuple.Create();

    for (int i = 0; i < currRow.Values.Length; i++)
    {
        if (currRow.Values[i] != null)
        {
            if (dataSet.Tables[0].ColumnNames[i].GetType().IsEquivalentTo(typeof(DateTime)))
            {
                DateTime value = DateTime.Parse(dataSet.Tables[0].ColumnNames[i].ToString());
                tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
            }
            else if (dataSet.Tables[0].ColumnNames[i].GetType().IsEquivalentTo(typeof(Double)))
            {
                Double value = Convert.ToDouble(dataSet.Tables[0].ColumnNames[i]);
                tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
            }
            else
            {
                string value = dataSet.Tables[0].ColumnNames[i].ToString();
                tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
            }
        }
    }
    inSpace_.Put(tuple);
}
foreach(dataSet.Tables[0].Rows中的var currRow)
{
var tuple=Com.Tibco.As.Space.tuple.Create();
对于(int i=0;i
如果您在编译时知道类型,请使用
Convert.ToDouble
Convert.ToInt32
等。如果您不知道,请使用
Convert.ChangeType
foreach(dataSet.Tables[0].Rows中的var currRow)
foreach (var currRow in dataSet.Tables[0].Rows)
{
    var tuple = Com.Tibco.As.Space.Tuple.Create();

    for (int i = 0; i < currRow.Values.Length; i++)
    {
        var obj = dataSet.Tables[0].ColumnNames[i], currRow.Values[i];
        var value = null;
        value = obj as double;
        if (!validObject(value)) value as string;
        if (!validObject(value)) value as DateTime;
        tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
    }

    inSpace_.Put(tuple);
}

bool validObject(object obj) { return (null != obj); }
{ var tuple=Com.Tibco.As.Space.tuple.Create(); 对于(int i=0;i 更新,根据您的编辑,我将使用开关打开类型:

for (int i = 0; i < currRow.Values.Length; i++)
{
    var type = obj.GetType();
    var obj = dataSet.Tables[0].ColumnNames[i], currRow.Values[i];
    var value = null;
    switch (type)
    {
        case DateTime:
            value = DateTime.Parse(obj);
            break;
        case double:
            value = Convert.ToDouble(obj);
            break;
        case string:
            value = Convert.ToString(obj);
            break;
    }
    tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
}
for(int i=0;i
我认为我们的思路是对的,但我不想只检查它是什么类型的。我想根据每个对象的类型对其进行不同的解析。因此,如果您获取type()并将其转换为datetime,我将像inSpace.Put(dataSet.Tables[0].ColumnNames[i],(datetime)currRow.Values[i])一样插入它;你试过第一版吗?它将根据它能够转换为的类型来转换obj。是的,我会尝试,你能看看我尝试转换时发布的新代码吗it@Ramie更新为备用version@Ramie你和哪一个一起去的?