当前位置:网站首页>C# 反射的使用
C# 反射的使用
2022-07-19 03:26:00 【廷益--飞鸟】
1.生成库程序集(Dll) ReflectionTestLab.dll
生成的dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReflectionTestLab
{
public enum MoveDir
{
left,
right,
up,
down,
}
public class Test
{
private int i = 1;
public int j = 0;
public string str = "123";
public MoveDir moveDir;
public Test()
{
}
public Test(int i)
{
this.i = i;
}
public Test(int i, MoveDir moveDir)
{
this.i = i;
this.moveDir = moveDir;
Console.WriteLine("调用构造方法 ---------------------- ");
}
public Test(int i, string str) : this(i)
{
this.str = str;
}
public void Speek()
{
System.Console.WriteLine(i.ToString());
}
public void Move()
{
System.Console.WriteLine("调用--Move");
}
public void Draw()
{
System.Console.WriteLine("调用--Draw");
}
public void Clear()
{
System.Console.WriteLine("调用--Clear");
}
}
}
2.使用反射 调用dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Lesson21_reflection
{
class Program
{
static void Main(string[] args)
{
#region Assembly
// 加载一个指定 程序集
Console.WriteLine("---加载程序集 Assembly" + "ReflectionTestLab.dll ------------------");
Assembly assemble = Assembly.LoadFrom(@"../../../ReflectionTestLab/bin/debug/ReflectionTestLab.dll");
// 同一个目录下
// Assembly.Load("ReflectionTestLab.dll");
Console.WriteLine("---获取 Types " + "------------------");
// 获取程序集中所有的 Types
Type[] types = assemble.GetTypes();
for (int i = 0; i < types.Length; i++)
{
Console.WriteLine(types[i]);
}
Console.WriteLine("---获取 Type " + "ReflectionTestLab.Test ------------------");
// 获取单个Type
Type testType = assemble.GetType("ReflectionTestLab.Test");
// 获取Type的 公共成员变量与方法
Console.WriteLine("---获取 memberInfos " + "testType GetMembers ------------------");
MemberInfo[] memberInfos = testType.GetMembers();
for (int i = 0; i < memberInfos.Length; i++)
{
Console.WriteLine(memberInfos[i]);
}
// 使用反射 获取枚举Type
Type typeDir = assemble.GetType("ReflectionTestLab.MoveDir");
FieldInfo dirRight = typeDir.GetField("right");
// 实例化对象
// 用于快速实例化对象的类
object testObj = Activator.CreateInstance(testType, 9, dirRight.GetValue(null));
Console.WriteLine("---获取方法 调用方法 " + "------------------");
// 获取对象的方法
MethodInfo move = testType.GetMethod("Move");
MethodInfo draw = testType.GetMethod("Draw");
MethodInfo clear = testType.GetMethod("Clear");
// 调用对象的方法
clear.Invoke(testObj, null);
move.Invoke(testObj, null);
draw.Invoke(testObj, null);
// 调用对象的 成员变量
Console.WriteLine("---读取成员变量 " + "------------------");
FieldInfo fieldStr = testType.GetField("str");
Console.WriteLine(fieldStr.GetValue(testObj));
#endregion
#region 总结 反射
// 1.在程序运行时,可以通过反射调用其它应用程序集 以及信息
// 2.信息包括 类、函数、变量、创建对象,实例化以及执行。
// 关键类 Type Assembly Activator
#endregion
Console.ReadLine();
}
}
}
边栏推荐
- 三、品达通用权限系统__pd-tools-swagger2
- 浅谈:Web3创造者经济
- MIT-6874-Deep Learning in the Life Sciences Week 8
- 支付宝统一下单支付接口(适用于H5、PC、APP)
- 2022广东网络安全省赛—代码渗透测试wp
- 【gclient sync】webrtc dial tcp 173.252.88.133:443: connectex: A connection attempt failed
- MIT-6874-Deep Learning in the Life Sciences Week 8
- Metaverse:元宇宙中DeFi的可能性和局限性
- 通过网上的客户经理办理炒股开户安全吗?
- 股票开户免费办理佣金最低的券商,网上开户安全吗
猜你喜欢
如何识别全部单号快递公司查询每个单号物流
【Runge-Kutta】龙格-库塔法求解微分方程matlab仿真
Rocketchip RISC-V Debug调试硬件相关(二)dmInner时钟和复位
Talking about: Web3 creator economy
MIT-6874-Deep Learning in the Life Sciences Week 8
Database system insider storage engine
CodisVSRedisCluster:我该选择哪一个集群方案?
Enterprise digital transformation: three key points!
Set集合学习
數據庫系統概論總結複習
随机推荐
修改csv
Debian侵犯Rust商标,妥协改名还是会得到豁免?
小样本学习-入门
Database system insider storage engine
[直播学习--2022/7/15]--nodejs脚本
Transaction recovery and processing of "inside database system"
Summary and review of introduction to database system
逆矩阵 分块矩阵
[Android开发学iOS系列] 语言篇: Swift vs Kotlin
C语言枚举类型和联合体
Enterprise digital transformation: three key points!
仆人式领导的八个注意事项
股票开户免费办理佣金最低的券商,网上开户安全吗
The principle of buffer in flux
自定义类型——枚举和联合
硅谷课堂-公众号云点播 硅谷课堂微服务项目实战笔记
《微信小程序-进阶篇》package.json必须掌握的字段知识(二)
01.有效的括号
Stock account opening is free of charge. Is it safe to open an account online
安装mysql时报错