职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1291|回复: 0

(转载)使用C#进行Reflection编程

[复制链接]
蓝色梦幻 发表于 2008-3-11 15:46 | 显示全部楼层 |阅读模式
代码如下:

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 nameisplayModules
method nameisplayTypes
method nameisplayMethods
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
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-9 11:01 , Processed in 0.129370 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表