使用C#进行Reflection编程

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 02:00   11   0
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

代码如下:

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.exedisplay types ...type name:TestReflectiontype name:TestClasstype name:TestSubClassdisplay methods in TestReflection Type ...method name:GetHashCodemethod name:Equalsmethod name:ToStringmethod name:DisplayModulesmethod name:DisplayTypesmethod name:DisplayMethodsmethod name:InvokeStaticMethodmethod name:InvokeMethodmethod name:Test2method name:GetTypeInvoke static method ...This is TestSubClass.SayHelloInvoke Method ...This is TestSubClass.Test1This is TestSubClass.Test2

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP