Reflect Demo

using System;

namespace ClassLibrary1
{
   public interface IWel
   {
      String Print();
   }

   public class Arithmetic : IWel
   {
      int _num1;
      public int Num1 { get { return _num1; } set { _num1 = value; } }
      int _num2;
      public int Num2 { get { return _num2; } set { _num2 = value; } }

      public Arithmetic() { }

      public Arithmetic(int num1, int num2)
      {
         _num1 = num1;
         _num2 = num2;
      }

      public String Add(int num1, int num2)
      {
         Console.WriteLine("{0}+{1}={2}", num1, num2, num1 + num2);
         return "Add(int num1,int num2) is a public method with param";
      //   return "Add(int num1,int num2)方法是一个公有的带参数的方法";
      }

      private string Add()
      {
         //return "Add()方法是一个私有的不传参数的方法";
         return "Add() is a private method without param";
      }

      private void Subtration(int num1, int num2)
      {
         Console.WriteLine("{0}-{1}+Subtration(int num1,int num2)" +
             "is a private method with param", num1, num2, num1 - num2);
            // "方法是一个私有的带参数的方法", num1, num2, num1 - num2);
      }

      public static void Multiplication(int num1, int num2)
      {
         Console.WriteLine("{0}*{1}={2} Multipcation(int num1,int num2)" +
              "is a public with static method", num1, num2, num1 * num2);
          //  "是一个公有的带参数的静态方法", num1, num2, num1 * num2);
      }

      private static void Multiplication()
      {
        // Console.WriteLine("Multiplication()是一个公有的不带参数的方法");
         Console.WriteLine("Multiplication() is a public without method");
      }

      #region Iwel 成员
      public string Print()
      {
         //return "欢迎你的使用接口";
         return "welcome to use your interface";
      }
      #endregion
   }

   class Program
   {
      static void Main(string[] args)
      {
      }
   }
}

 

****************************************************************************

using

 

 ClassLibrary1;

 

using

 

 System;

 

using

 

 System.Reflection;

 

 

 

namespace

 

 test

 

{

  

 

class Program

 

   {

     

 

delegate void TestDelegage(int num1, int num2);

 

     

 

static void Main(string[] args)

 

        {

          

 

//Console.OutputEncoding = Encoding.GetEncoding("utf-8");

 

          

 

//Console.WriteLine(Console.OutputEncoding.ToString());

 

          

 

// Console.WriteLine(Console.OutputEncoding.CodePage);

 

           

 

Assembly assembly = Assembly.Load("ClassLibrary1");

 

          

 

// Console.WriteLine("得到这个里面所有的类");

 

           

 

Console.WriteLine("Get all the class in the assembly");

 

           

 

//foreach (Type var in assembly.GetTypes())

 

           

 

//{

 

           

 

//    Console.WriteLine(var.Name + "是ClassLibrary1命名空间下的类");

 

           

 

//}

 

           

 

foreach (Type var in assembly.GetTypes())

 

            {

              

 

Console.WriteLine(var.Name + "is a class in namespace ClassLibrary1");

 

            }

           

 

Console.WriteLine();

 

          

 

// Console.WriteLine("得到这个里面所有的模块集");

 

           

 

Console.WriteLine("Get all the module in the assembly");

 

           

 

Module[] modules = assembly.GetModules();

 

           

 

foreach (Module va in modules)

 

            {

               

 

Console.WriteLine("Module name is that: "+va.Name);

 

            }

           

 

Console.WriteLine();

 

           

 

Type type = typeof(Arithmetic);

 

          

 

// Console.WriteLine("{0}是不是public类型{1}", type, type.IsPublic);

 

           

 

Console.WriteLine("{0} is public type {1} : ", type, type.IsPublic);

 

           

 

PropertyInfo[] memberinfo = type.GetProperties();//得到类的属性

 

           

 

foreach (PropertyInfo var in memberinfo)

 

            {

             

 

//  Console.WriteLine(type + "类的属性有" + var.Name);

 

              

 

Console.WriteLine(type + "class' property : " + var.Name);

 

            }

           

 

Console.WriteLine();

 

           

 

Type[] t=type.GetInterfaces();

 

           

 

foreach(Type var in t)

 

            {

               

 

//Console.WriteLine(var.Name+"是接口");

 

              

 

Console.WriteLine(var.Name + " is interface");

 

            }

           

 

Console.WriteLine();

 

           

 

MethodInfo[] method = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);

 

           

 

foreach (MethodInfo var in method)

 

            {

              

 

Console.WriteLine("private:Method name =: {0} method infomation=: {1}", var.Name, var);

 

               

 

//Console.WriteLine("私有方法:方法名={0} 方法信息={1}", var.Name, var);

 

            }

           

 

Console.WriteLine();

 

           

 

MethodInfo[] methodpublic = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);

 

           

 

foreach (MethodInfo var in methodpublic)

 

            {

               

 

//Console.WriteLine("公有方法:方法名={0} 方法信息{1}", var.Name, var);

 

              

 

Console.WriteLine("public method: method name=: {0} method infomation: {1}", var.Name, var);

 

            }

           

 

Console.WriteLine();

 

           

 

MethodInfo[] methodstatic = type.GetMethods(BindingFlags.Public | BindingFlags.Static);

 

           

 

foreach (MethodInfo var in methodstatic)

 

            {

             

 

//  Console.WriteLine("静态公有方法名={0} 方法信息={1}", var.Name, var);

 

              

 

Console.WriteLine("static public method=: {0} method infomation=: {1}", var.Name, var);

 

            }

           

 

Console.WriteLine();

 

           

 

MethodInfo[] methodprivatestatic = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static);

 

           

 

foreach(MethodInfo var in methodprivatestatic)

 

            {

               

 

//Console.WriteLine("静态私有方法={0} 方法信息{1}",var.Name,var);

 

              

 

Console.WriteLine("static private method=: {0} method infomation: {1}", var.Name, var);

 

            }

           

 

Console.WriteLine();

 

           

 

Console.WriteLine("Constructor is listed as follow:");

 

           

 

ConstructorInfo[] con=type.GetConstructors();

 

           

 

foreach(ConstructorInfo var in con)

 

            {

               

 

Console.WriteLine(var.Name +"/ /" + var);

 

            }

           

 

Console.WriteLine();

 

           

 

object obj=Activator.CreateInstance(type, null);

 

           

 

MethodInfo men1=type.GetMethod("Add");

 

           

 

Console.WriteLine("invoke {0} method",men1);

 

           

 

object[] nums1={5,4};

 

           

 

Console.WriteLine(men1.Invoke(obj,nums1));

 

           

 

Console.WriteLine();

 

           

 

MethodInfo men2 = type.GetMethod("Add", BindingFlags.Instance|BindingFlags.Public);

 

           

 

Console.WriteLine(men2.Invoke(obj, nums1));

 

           

 

Console.WriteLine();

 

           

 

MethodInfo men3=type.GetMethod("Multiplication",BindingFlags.Public|BindingFlags.Static);

 

           

 

object[] nums2={5,6};

 

           

 

men3.Invoke(null,nums2);

 

           

 

Console.WriteLine("************************");

 

           

 

MethodInfo men4=type.GetMethod("Multiplication",BindingFlags.Static|BindingFlags.NonPublic);

 

           

 

men4.Invoke(null,null);

 

           

 

Console.WriteLine();

 

           

 

Console.WriteLine("************************");

 

           

 

TestDelegage dele=(TestDelegage)Delegate.CreateDelegate(typeof(TestDelegage),obj,"Subtration");

 

           

 

dele(9,3);

 

 

           

 

Console.WriteLine();

 

           

 

Console.ReadLine();

 

        }

   }

}

 

 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值