c#-反射学习笔记2

代码

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);
            }
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值