代码
using System;
using System.Reflection;
class Af{
public int af = 10;
protected int bf = 20;
public void funf(){
}
protected void funfProtected(){
}
public event EventHandler<AEventArgs> AInfoF;
protected event EventHandler<AEventArgs> AInfoFprotected;
}
class AEventArgs:EventArgs{
}
class A : Af{
public int a=1;
int b=2;
private int c = 3;
public void fun1(){
}
void fun2(){
}
public event EventHandler<AEventArgs> AInfo;
}
class HelloWorld
{
static void Main(string[] args)
{
HelloWorld hw = new HelloWorld();
hw.mian();
}
private void mian(){
Console.WriteLine("反射实验");
Type t = Type.GetType("A");
// 无参构造
A a = (A)Activator.CreateInstance(t);
Field(t,a);
Method(t,a);
//Property(t,a);
Member(t,a);
Event(t,a);
}
private void Field(Type t,Object o){
Console.WriteLine("\nField:");
foreach(FieldInfo fi in t.GetFields()){
//Console.WriteLine(fi.GetType().Name);
Console.WriteLine(fi.Name);
}
}
private void Property(Type t,Object o){
Console.WriteLine("\nPropertie:");
foreach (PropertyInfo pi in t.GetProperties()){
//Console.WriteLine(pi.GetValue(o, null));
//Console.WriteLine(pi.GetType().Name);
Console.WriteLine(pi.Name);
}
}
private void Member(Type t,Object o){
Console.WriteLine("\nMember:");
foreach(MemberInfo mi in t.GetMembers()){
//Console.WriteLine(mi.GetType().Name);
Console.WriteLine(mi.Name);
}
}
private void Method(Type t,Object o){
Console.WriteLine("\nMethod:");
foreach(MethodInfo mi in t.GetMethods()){
//Console.WriteLine(mi.GetType().Name);
Console.WriteLine(mi.Name);
}
}
private void Event(Type t,Object o){
Console.WriteLine("\nEvent:");
foreach(EventInfo mi in t.GetEvents()){
//Console.WriteLine(mi.GetType().Name);
Console.WriteLine(mi.Name);
}
}
}
运行效果


规律总结

属性(property)
// 属性
class A {
public int As { set; get; }
public void fun() {
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Program p = new Program();
A a = new A();
p.Property(a);
Console.ReadLine();
}
private void Property(Object o)
{
Type t = o.GetType();
Console.WriteLine("\nPropertie:");
foreach (PropertyInfo pi in t.GetProperties())
{
//Console.WriteLine(pi.GetValue(o, null));
//Console.WriteLine(pi.GetType().Name);
Console.WriteLine(pi.Name);
}
}
}
}

456

被折叠的 条评论
为什么被折叠?



