如题,
public class TestClassA{public string arg1=@"123";
public string arg2=@"abcdefg";
public int argI=5623;
public double argD=234.562d;
}//class
public void EnumTheClass(object m_Class_A){
... ...
//如何通过此函数,直接把arg1,arg2,argI,argD这几个变量 名称,类型,内容 探测出来,
//并用for等循环语句进行循环呢?
}//
public class TestClassA{public string arg1=@"123";
public string arg2=@"abcdefg";
public int argI=5623;
public double argD=234.562d;
}//class
public void EnumTheClass(object m_Class_A){
... ...
//如何通过此函数,直接把arg1,arg2,argI,argD这几个变量 名称,类型,内容 探测出来,
//并用for等循环语句进行循环呢?
}//
{
EnumTheClass(new TestClassA());
}
public void EnumTheClass(object m_Class_A)
{
if (m_Class_A is TestClassA)
{
System.Type t = m_Class_A.GetType();
PropertyInfo[] pis = t.GetProperties();
foreach (PropertyInfo pi in pis)
{
Response.Write(pi.Name + "<br>");
}
}
}
///
/// loads a *.dll file from txtMethods and invokes all methods int it.
/// lists all types in the assembly
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnList_Click(object sender, System.EventArgs e)
{ string fileName=labelFile.Text.Trim();
string result="";
txtMethods.Text="";
txtTypes.Text=""; if(File.Exists(fileName))
{
try
{
Assembly assembly=Assembly.LoadFrom(fileName);
Type[] types=assembly.GetTypes();
result="The Assembly contains the following types :"+Environment.NewLine; for(int i=0;i<types.GetLength(0);++i)
{
result+="\t "+i+":"+types[i].Name+" "+" "+Environment.NewLine; // Get the public methods.
MethodInfo[] myArrayMethodInfo=types[i].GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);
txtMethods.Text=txtMethods.Text+Environment.NewLine+"The number of public methods in "+types[i].Name+" is "+myArrayMethodInfo.Length+Environment.NewLine;
// get all the methods.
txtMethods.Text=txtMethods.Text+getMethodInfo(myArrayMethodInfo);
/*
// Get the nonpublic methods.
MethodInfo[] myArrayMethodInfo1 = myType.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
Console.WriteLine("\nThe number of protected methods is {0}.", myArrayMethodInfo1.Length);
// Display information for all methods.
LabelFile.Text=DisplayMethodInfo(myArrayMethodInfo1);
*/ } foreach(Type myType in types)
{
// Get the public properties.
PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
Console.WriteLine("The mumber of public properties in "+myType.Name+" is {0}.", myPropertyInfo.Length);
// Display the public properties.
getPropertyInfo(myPropertyInfo);
// Get the nonpublic properties.
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
txtMethods.Text=txtMethods.Text+Environment.NewLine+("The number of NonPublic properties in "+myType.Name+" is "+ myPropertyInfo1.Length)+Environment.NewLine;
// Display all the nonpublic properties.
txtMethods.Text=txtMethods.Text+getPropertyInfo(myPropertyInfo1);
}
txtTypes.Text=result;
}
catch(Exception ee)
{
throw ee;
} } } /// <summary>
/// get Method informations from MethodInfo[] Array:
/// </summary>
/// <param name="myArrayMethodInfo"></param>
/// <returns></returns>
public string getMethodInfo(MethodInfo[] myArrayMethodInfo)
{
string methodStr="";
///
///getinformation for all methods.
for(int i=0;i<myArrayMethodInfo.Length;i++)
{
MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
methodStr+="Method "+i+" :"+ myMethodInfo.Name+Environment.NewLine;
}
return methodStr;
} /// <summary>
/// get properties information from PropertyInfo[] Array:
/// </summary>
/// <param name="myPropertyInfo"></param>
/// <returns></returns>
public string getPropertyInfo(PropertyInfo[] myPropertyInfo)
{
string propStr="";
// Display information for all properties.
for(int i=0;i<myPropertyInfo.Length;i++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
propStr+="property "+i+":"+ myPropInfo.Name+" type:"+ myPropInfo.PropertyType+Environment.NewLine;
}
return propStr;
}
{
EnumTheClass(new TestClassA());
}
public void EnumTheClass(object m_Class_A)
{
if (m_Class_A is TestClassA)
{
System.Type t = m_Class_A.GetType();
object m = Activator.CreateInstance(t); FieldInfo[] fis = t.GetFields();
foreach (FieldInfo fi in fis)
{
Response.Write("Field Name:"+fi.Name +" Value:"+fi.GetValue(m).ToString()+ "<br>");
}
}
}
{
EnumTheClass(new TestClassA());
}
public void EnumTheClass(object m_Class_A)
{
if (m_Class_A is TestClassA)
{
System.Type t = m_Class_A.GetType();
object m = Activator.CreateInstance(t); FieldInfo[] fis = t.GetFields(BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.DeclaredOnly|BindingFlags.Instance);
foreach (FieldInfo fi in fis)
{
Response.Write("Field Name:"+fi.Name +" Value:"+fi.GetValue(m).ToString()+ "<br>");
}
}
}
MSDN 上有很多例子
去收一把
{
FieldInfo[] loginSessionFieldInfo = typeof(LoginSession).GetFields();
foreach(FieldInfo fi in loginSessionFieldInfo)
{
String temp = (string)fi.GetValue(null);//取值
Session[temp] = dataRow[temp].ToString();
}
}
using System.Reflection
using System.Reflection.Emit
可以把Class先编译成程序集
然后利用反射机制 对程序的元数据进行查询。
Type---Class