【问题标题】:c# how to get the result string from objectc#如何从对象中获取结果字符串
【发布时间】:2013-01-24 06:52:05
【问题描述】:

一些 c# 代码,例如:

Array arr=Array.CreateInstance(typeof(oject),3);
bool b=true;
int i =2;
float[] foo={1.1f,2.2f};
arr.setValue(b,0);
arr.setValue(i,0);
arr.setValue(foo,0);
string str=GetParamStringFromArrayObject(arr.GetValue(3));

string GetParamStringFromArrayObject(object obj)
{
if(obj.GetType().IsArray)
{
int demesion=obj.GetType().GetRank();//error,how to get the demesion(should be 2)
return obj.ToString();//error,i want to return the string delimeter by ' ' of   the float array,how to do?
}
}

如上面的代码,如果我想要对象中的字符串值是一个数组,我怎样才能得到它,以及如何得到数组对象的demesion?

非常感谢。

【问题讨论】:

    标签: c# object object-to-string


    【解决方案1】:

    您必须将对象转换为数组,如下所示:

    string GetParamStringFromArrayObject(object obj)
    {
       Array array = obj as Array;
       if (array != null)
       {
           int demesion = array.Rank;
           // etc.
        }
    }
    

    【讨论】:

      【解决方案2】:
      if(obj is Array)
      {
         int size = (obj as Array).Length; // get size
         string items = ""; // get string
         foreach (object item in obj as Array)
           items += item.ToString() + ",";
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-04
        • 2023-03-28
        • 1970-01-01
        • 2013-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-22
        • 2012-03-24
        相关资源
        最近更新 更多