zoukankan      html  css  js  c++  java
  • 使用C#进行Reflection编程

    using System;   
  • using System.Reflection;   
  • using System.Reflection.Emit;   
  • public class TestReflection   
  • {   
  •      private String file = @"TestReflection.exe";   
  •      static void Main(String[] args)   
  •      {   
  •           TestReflection test = new TestReflection();   
  •           test.DisplayModules();   
  •           test.DisplayTypes();   
  •           test.DisplayMethods();   
  •           test.InvokeStaticMethod();   
  •           test.InvokeMethod();   
  •      }   
  •      //显示所有模块名   
  •      public void DisplayModules()   
  •      {   
  •           Console.WriteLine("display modules ...");   
  •           Assembly assembly = Assembly.LoadFrom(file);   
  •           Module[] modules = assembly.GetModules();   
  •           foreach (Module module in modules)   
  •           {   
  •                 Console.WriteLine("module name:" + module.Name);   
  •           }   
  •      }   
  •      //显示所有类型名   
  •      public void DisplayTypes()   
  •      {   
  •           Console.WriteLine("display types ...");   
  •           Assembly assembly = Assembly.LoadFrom(file);   
  •           Type[] types = assembly.GetTypes();   
  •           foreach (Type type in types)   
  •           {   
  •                 Console.WriteLine("type name:" + type.FullName);   
  •           }   
  •      }   
  •      //先是一个类型中的所有方法名   
  •      public void DisplayMethods()   
  •      {   
  •           Console.WriteLine("display methods in TestReflection Type ...");   
  •           Assembly assembly = Assembly.LoadFrom(file);   
  •           Type type = assembly.GetType("TestReflection");   
  •           MethodInfo[] methods = type.GetMethods();   
  •           foreach (MethodInfo method in methods)   
  •           {   
  •                 Console.WriteLine("method name:" + method.Name);   
  •           }   
  •      }   
  •      //调用一个类中的静态方法   
  •      public void InvokeStaticMethod()   
  •      {   
  •           Console.WriteLine("Invoke static method ...");   
  •           Assembly assembly = Assembly.LoadFrom(file);   
  •           Type type = assembly.GetType("TestSubClass");   
  •           type.InvokeMember("SayHello", BindingFlags.InvokeMethod, nullnullnew object[] { });   
  •      }   
  •      //调用一个类中的非静态方法   
  •      public void InvokeMethod()   
  •      {   
  •           Console.WriteLine("Invoke Method ...");   
  •           Assembly assembly = Assembly.LoadFrom(file);   
  •           Type type = assembly.GetType("TestSubClass");   
  •           Object obj = Activator.CreateInstance(type);   
  •           TestClass tc = (TestClass)obj;   
  •           type.InvokeMember("Test1", BindingFlags.InvokeMethod, null, tc, new object[] { });   
  •           type.InvokeMember("Test2", BindingFlags.InvokeMethod, null, tc, new object[] { });   
  •      }   
  • }   
  • public interface TestClass   
  • {   
  •      void Test1();   
  •      void Test2();   
  • }   
  • public class TestSubClass : TestClass   
  • {   
  •      public void Test1()   
  •      {   
  •           Console.WriteLine("This is TestSubClass.Test1");   
  •      }   
  •      public void Test2()   
  •      {   
  •           Console.WriteLine("This is TestSubClass.Test2");   
  •      }   
  •      public static void SayHello()   
  •      {   
  •           Console.WriteLine("This is TestSubClass.SayHello");   
  •      }   
  • }  
查看全文
  • 相关阅读:
    CVTE 一面
    【玩转Ubuntu】08. Linux报错:Syntax error: "(" unexpected解决办法
    Advanced Replication同步复制实验(基于Trigger&基于Materialized View)
    centos6.4-x86-64系统更新系统自带Apache Http Server
    针对某个表使用高级复制进行数据同步示例
    [置顶] What is the difference between Category and Class Extension?
    Boxes And Balls(三叉哈夫曼编码)
    xorequation(DFS完全枚举)
    Dreamoon and MRT(二元枚举)
    矩阵链乘(解析表达式)
  • 原文地址:https://www.cnblogs.com/kingwangzhen/p/1791604.html
  • Copyright © 2011-2022 走看看