111,126
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}
}
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));
}
}
}
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]);
}
}
}
}
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()
));
}
}