对基本类的反射,获取到该类的所有变量定义,函数等信息
private void button1_Click(object sender, EventArgs e)
{
Type t = typeof(People);
richTextBox1.AppendText("\r"+"----------------Method------------------");
MethodInfo[] methods = t.GetMethods();
foreach
(MethodInfo method in methods)
{
richTextBox1.AppendText(‘\r‘ + "Method:" + method);
richTextBox1.AppendText(string.Format(‘\r‘ + "method name:{0} returnValue:{1}",
method.Name, method.ReturnType));
ParameterInfo[] pi =
method.GetParameters();
foreach (ParameterInfo pinfo in
pi)
{
richTextBox1.AppendText(string.Format("\r" + "Parameter name:{0}
Type:{1}",pinfo.Name,pinfo.ParameterType));
}
richTextBox1.AppendText("\n");
}
richTextBox1.AppendText("\r"+"---------------Field-------------------");
FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static);
foreach (FieldInfo
field in fields)
{
richTextBox1.AppendText("\r"+"Field:" + field);
richTextBox1.AppendText(string.Format("\r" + "Field name:{0} type:{1}" ,
field.Name,field.FieldType));
}
richTextBox1.AppendText("\r"+"--------------Member--------------------");
MemberInfo[] members = t.GetMembers();
foreach
(MemberInfo member in members)
{
richTextBox1.AppendText("\r"+"Member:" + member);
}
richTextBox1.AppendText("\r"+"--------------Property--------------------");
PropertyInfo[] properties = t.GetProperties();
foreach
(PropertyInfo property in properties)
{
richTextBox1.AppendText("\r"+"Property:" + property);
}
richTextBox1.AppendText("\r"+"--------------Constructor--------------------");
ConstructorInfo[] constructors =
t.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (ConstructorInfo constructor in constructors)
{
richTextBox1.AppendText("\r"+"Constructor:" + constructor);
}
}
基础类定义
public class People //类名
{
private static string name; //字段
private string sex;//字段
public string Sex //属性
{
get {
return sex; }
set { sex = value; }
}
public
static string Name //属性
{
get { return People.name; }
set { People.name = value; }
}
private People() //构造函数
{
}
public static string GetName() // 函数
{
if
(string.IsNullOrEmpty(name))
{
name = "my
name";
}
return name;
}
}
执行结果如下:
----------------Method------------------
Method:System.String
get_Sex()
method name:get_Sex returnValue:System.String
Method:Void set_Sex(System.String)
method name:set_Sex
returnValue:System.Void
Parameter name:value Type:System.String
Method:System.String get_Name()
method name:get_Name
returnValue:System.String
Method:Void set_Name(System.String)
method name:set_Name
returnValue:System.Void
Parameter name:value Type:System.String
Method:System.String GetName()
method name:GetName
returnValue:System.String
Method:System.String ToString()
method name:ToString
returnValue:System.String
Method:Boolean Equals(System.Object)
method name:Equals
returnValue:System.Boolean
Parameter name:obj Type:System.Object
Method:Int32 GetHashCode()
method name:GetHashCode
returnValue:System.Int32
Method:System.Type GetType()
method name:GetType
returnValue:System.Type
---------------Field-------------------
Field:System.String sex
Field
name:sex type:System.String
Field:System.String name
Field name:name
type:System.String
--------------Member--------------------
Member:System.String
get_Sex()
Member:Void set_Sex(System.String)
Member:System.String
get_Name()
Member:Void set_Name(System.String)
Member:System.String
GetName()
Member:System.String ToString()
Member:Boolean
Equals(System.Object)
Member:Int32 GetHashCode()
Member:System.Type
GetType()
Member:System.String Sex
Member:System.String
Name
--------------Property--------------------
Property:System.String
Sex
Property:System.String
Name
--------------Constructor--------------------
Constructor:Void
.ctor()