如何将ojbect实例中的内容提取到一个字符串数组中?

paddy102 2005-09-03 10:14:29
在程序中,会根据查询语句得到相应的结果,形式如下:
string sql = "select * from TUser tu where tu.FUserId > 1"; // 查询实体
IList items = GetObjects(sql); // 这个方法是自定义的
经过这个查询,items中是一个个的object,这个时候,我要把object中的内容转换到一个字符串数组中,应该如何操作?

如果查询语句如下:
string sql = "select tu.FUserName, tu.FUserBirth from TUser tu where tu.FUserId > 1"; // 查询属性
IList items = GetObjects(sql);
这个时候,返回,items中是一个个的object[],可以通过下面代码输出其值内容:
object[] rows = (object[])items[0];
Console.WriteLine(rows[0] + "__" + rows[1]);


朋友们,过来看看,出出主意
...全文
106 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
paddy102 2005-09-03
  • 打赏
  • 举报
回复
using System.Reflection;


string sql = "select * from TUser tu where tu.FUserId > 1"; // 查询实体
IList items = GetObjects(sql); // 这个方法是自定义的

for ( int i = 0; i < items.Count; i++ )
{
if ( items[i].GetType().ToString() == "System.Object[]" )
{
object[] rows = (object[])items[i];
string[] strTemp = new string[rows.GetLength(0)];
int j = 0;
while ( j < rows.GetLength(0) )
{
strTemp[j] = rows[j];
j++;
}
}
else
{
Type theType = items[i].GetType();
PropertyInfo[] infoArray = theType.GetProperties();
foreach ( PropertyInfo info in infoArray )
{
Console.WriteLin(info.GetValue(items[i], null));
}
}

这段代码可以解决问题
paddy102 2005-09-03
  • 打赏
  • 举报
回复
JasonHeung(拥有一切也不过就这样笑着哭) 的代码反映的是第二种情形,即是查询属性的时候,结果返回ojbect[],这个时候可以获取object成员值内容.
object[] rows = (object[])items[0];
Console.WriteLine(rows[0] + "__" + rows[1]); // all right
paddy102 2005-09-03
  • 打赏
  • 举报
回复
GetObjects返回IList, System.Collections中的集合类型
wuyi8808 2005-09-03
  • 打赏
  • 举报
回复
你的 GetObjects() 返回类型是什么?
JasonHeung 2005-09-03
  • 打赏
  • 举报
回复
string[] ss = new string[rows.GetLength(0)];
int i;
for (i = 0 ; i < row.GetLength(0); ++i)
{
ss[i] = rows[i].ToString();
}
TechEye 2005-09-03
  • 打赏
  • 举报
回复
那么复杂干嘛,这样

IList items = GetObjects(sql); // 这个方法是自定义的
ArrayList ary = new ArrayList();
ary.AddRange(items);

//这样就转换完了
string[] s = (string[])ary.ToArray(typeof(string));

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧