使用C#進行Reflection編程

來源:互聯網
上載者:User

代碼如下:

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,null,null,new 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");
 }
}

編譯運行後輸出:

display modules ...
module name:TestReflection.exe
display types ...
type name:TestReflection
type name:TestClass
type name:TestSubClass
display methods in TestReflection Type ...
method name:GetHashCode
method name:Equals
method name:ToString
method name:DisplayModules
method name:DisplayTypes
method name:DisplayMethods
method name:InvokeStaticMethod
method name:InvokeMethod
method name:Test2
method name:GetType
Invoke static method ...
This is TestSubClass.SayHello
Invoke Method ...
This is TestSubClass.Test1
This is TestSubClass.Test2

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.