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-如何遍历一个实例类里所有的成员

解决方案 »

  1.   

    list 
    集合
    如何遍历一个实例类里所有的成员 用foreach 
      

  2.   

    只要遍历DataTable里的数据就可以了
    不用在把值都放到枚举里了
    因为datatable里的数据的作用就是枚举
      

  3.   

    private void PrintRows(DataSet dataSet)
    {
        // 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]);
                }
            }
        }
    }
      

  4.   

    第2个问题用反射,下面是我以前写的一段代码public void getField(Object o )
    {
        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));
            }
        }
    }
      

  5.   

    for(int i = 0;i<table1.Rows.Count;i++)
    {
       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]
                  }
            }你试一下看看,没测试,也许方法不是最好的,等待学习
      

  6.   

    兄弟,这样做有意义吗?再说这样是不可能的,除非你动态编译第2个问题嘛,可以用反射,你查一下MSDN很简单的
      

  7.   

    winform的例子:using System;
    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);
                }        }    }
    }
      

  8.   

    感谢,第二个问题可能我没说得很清楚我是希望在对一个实体类赋值之后,用一个方法来遍历这个实体类里所有的成员,得到这个值。比如:Model()是一个实体类,我希望能得到里面每个成员的值,而不是名字。谢谢
      

  9.   


    Impossible is nothing! 两件事情都可以实现,第一种可以通过动态编译实现,但是实现确实没有太大的实际意义~~(恐怕楼主要的也不是这个答案,楼主恐怕只是思路上有点问题),第二种既然对象可以序列化,那么也肯定是可以实现的~~ 一下,有时间的话写个代码。
      

  10.   


            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
      

  11.   

    enum 枚举是成员共享内存,你要遍历就用map映射之类,一个key,一个value刚好满足你的要求。
    也可以弄个2维数组,或者其他容器之类。