使用C#进行Reflection编程

博客展示了一段C#代码,利用反射机制实现显示模块名、类型名、方法名,以及调用静态和非静态方法。代码定义了相关类和接口,编译运行后输出了各操作的结果,如模块名、类型名、方法名及方法调用的输出内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值