1-怎么枚举DATATABLE里的东西?
比如有个Datatable里面有两列,10行,如何把第一列的所有数据全部放在一个枚举里面??而第二列作为他的值,不知道说清楚没有。 如:datatable
name number
a 1
b 2
c 3
d 4把这个Datatable放枚举里面 等效于:
public enum TTTT
{ a= 1,
b= 2,
c= 3,
d= 4,
};
2-如何遍历一个实例类里所有的成员
比如有个Datatable里面有两列,10行,如何把第一列的所有数据全部放在一个枚举里面??而第二列作为他的值,不知道说清楚没有。 如:datatable
name number
a 1
b 2
c 3
d 4把这个Datatable放枚举里面 等效于:
public enum TTTT
{ a= 1,
b= 2,
c= 3,
d= 4,
};
2-如何遍历一个实例类里所有的成员
解决方案 »
- JMail在64位服务器上失败,如何解决?
- 怎么认定才能让GridView中字体大小不随所用的浏览器字体的大小变化而变化
- 专家请进,关于Winform软件项目开发中datagridview的一个高级应用问题!
- 读取数据
- 向做打印编程的高手请教,关于识别打印机的问题
- C# 插入数据库 有代码不知道怎么改拉```大家帮忙``
- [原创]今天用Delphi和C#分别做了一个全国天气预报WSDL(Web Services)的调用例程序(含全部Source)
- 请告诉我一下
- 多文档窗体求救!
- 如何使工具栏的按钮变为平的啊?
- 高手一定会的C#程序?
- partial calss? 允许相同类名? 这样又有什么用呢?
集合
如何遍历一个实例类里所有的成员 用foreach
不用在把值都放到枚举里了
因为datatable里的数据的作用就是枚举
{
// For each table in the DataSet, print the values of each row.
foreach(DataTable thisTable in dataSet.Tables)
{
// For each row, print the values of each column.
foreach(DataRow row in thisTable.Rows)
{
foreach(DataColumn column in thisTable.Columns)
{
Console.WriteLine(row[column]);
}
}
}
}
{
this.type = o.GetType();
foreach (FieldInfo fi in t.GetFields(bf))
{
Type fieldType = fi.FieldType;
Console.WriteLine("FieldInfo : " + fi.FieldType + " " + fi.Name);
if (fieldType.IsValueType || fieldType == typeof(String))
{
Console.WriteLine("String: "+fi.GetValue(o));
}
else if(fieldType.IsArray)
{
//如果是数组的话,进行处理
}
else
{
//如果是类的话,递归调用函数
getField(fi.GetValue(o));
}
}
}
{
string[] str = new string[table1.Rows.Count]
string[] str1 = new string[table1.Rows.Count]
str[0] = table1.Rows[i][0];
str1[1] = table1.Rows[i][1];
}
public enum TTTT
{ for(int i = 0;i<str.Length;i++)
{
str[i] = str1[i]
}
}你试一下看看,没测试,也许方法不是最好的,等待学习
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Type t = typeof(Form1);
MethodInfo[] info = t.GetMethods();
foreach (MethodInfo i in info)
{
MessageBox.Show(i.Name);
} } }
}
Impossible is nothing! 两件事情都可以实现,第一种可以通过动态编译实现,但是实现确实没有太大的实际意义~~(恐怕楼主要的也不是这个答案,楼主恐怕只是思路上有点问题),第二种既然对象可以序列化,那么也肯定是可以实现的~~ 一下,有时间的话写个代码。
public class Class1
{
public int x1 = 100;
protected int x2 = 200;
private int x3 = 300;
protected static int x4 = 400; public Class2 o1 = null;
protected Class2 o2 = null;
private Class2 o3 = null;
protected static Class2 o4 = null;
} public class Class2
{ } static void Main()
{
object obj = new Class1(), ret = null;
Type objType = obj.GetType(); //共有成员、保护成员、私有成员
foreach (System.Reflection.FieldInfo field in objType.GetFields(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public))
{
System.Diagnostics.Debug.WriteLine(string.Format("{0}:{1}",
field.Name, (ret = field.GetValue(obj)) == null ? "null" : ret.ToString()
));
}
//静态成员
foreach (System.Reflection.FieldInfo field in objType.GetFields(System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public))
{
System.Diagnostics.Debug.WriteLine(string.Format("{0}:{1}",
field.Name, (ret = field.GetValue(obj)) == null ? "null" : ret.ToString()
));
}
}
输出结果是:
x1:100
x2:200
x3:300
o1:null
o2:null
o3:null
x4:400
o4:null
也可以弄个2维数组,或者其他容器之类。