代码改变世界

[原创]反射,.NET,委托

2007-08-15 17:30  Virus-BeautyCode  阅读(434)  评论(3编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace ConsoleApp
{
    public delegate string dele(string str);

    public class Bmw
    {
        private static int idata;
        private char cdata;
        public static string sdata;
        public double ddata;

        public Bmw()
        { }
        public Bmw(int i)
        {
            idata = i;
        }
        private Bmw(char c)

        {
            cdata = c;
        }

        public char Cdata
        {
            get
            { return cdata; }
            set
            { cdata = value; }
        }

        private string Ha()
        {
            Console.WriteLine("haha");
            return "";
        }

        public string Display(string str)
        {
            return "asdf";
        }

        public void Drive()
        {
            Console.WriteLine("drive bmw..");
        }
    }

 

    class Program
    {     
        static void Main()
        {
           
            //Assembly.Load().CreateInstance()
            Assembly a = Assembly.LoadFrom("ConsoleApp.exe");
            Type[] mytypes = a.GetTypes();

            Console.WriteLine("these types are included...");

            foreach (Type t in mytypes)
            {
                System.Console.WriteLine(t.Name);
            }

            Type ht = typeof(Bmw);

            Console.WriteLine("these methods are included..");

            MethodInfo[] info = ht.GetMethods();

            foreach (MethodInfo m in info)
            {
                Console.WriteLine(m.Name);
            }

            ConstructorInfo[] consinfo = ht.GetConstructors();
            Console.WriteLine(".................................................");
            Console.WriteLine("constructor is");
            foreach (ConstructorInfo c in consinfo)
            {
                Console.WriteLine(c.Name);
            }

            EventInfo[] einfo = ht.GetEvents();
            Console.WriteLine(".................................................");
            Console.WriteLine("event is");
            foreach (EventInfo e in einfo)
            {
                Console.WriteLine(e.Name);
            }

            FieldInfo[] finfo = ht.GetFields();
            Console.WriteLine(".................................................");
            Console.WriteLine("field is");
            foreach (FieldInfo f in finfo)
            {
                Console.WriteLine(f.Name);
            }

            MemberInfo[] minfo = ht.GetMembers();
            Console.WriteLine(".................................................");
            Console.WriteLine("member is");
            foreach (MemberInfo m in minfo)
            {
                Console.WriteLine(m.Name);
            }

            PropertyInfo[] pinfo = ht.GetProperties();
            Console.WriteLine(".................................................");
            Console.WriteLine("property is");
            foreach (PropertyInfo p in pinfo)
            {
                Console.WriteLine(p.Name);
            }

            Console.WriteLine(ht.GetMethod("Display").ToString());

            Bmw B=new Bmw();

            dele del = new dele(B.Display);

            Console.WriteLine(del("dd"));
            //Car car = Drive.DriveCar("benz");
            //car.Drive();
            Console.ReadLine();
            //System.Collections.Generic.
        }
    }
}