利用 MethodBody类的GetILAsByteArray方法可以获取到返回字节数组的MSIL的body。然后再去解析此字节数组, 可以得到MSIL,然后你再去解析MSIL,你就可以得到你想到source code,这样就可以做小的反....,以下是代码的实现....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit; namespace Timwi.ILReaderExample
{
public class ILReader
{
public class Instruction
{
public int StartOffset
{
get; private set;
}
public OpCode OpCode
{
get; private set;
}
public long? Argument
{
get; private set;
}
public Instruction(int startOffset, OpCode opCode, long? argument)
{
StartOffset = startOffset;
OpCode = opCode;
Argument = argument;
}
public override string ToString()
{
return OpCode.ToString() + (Argument == null ? string.Empty : " " + Argument.Value);
}
} private Dictionary<short, OpCode> _opCodeList; public ILReader()
{
_opCodeList = typeof(OpCodes).GetFields().Where(f => f.FieldType == typeof(OpCode)).Select(f => (OpCode)f.GetValue(null)).ToDictionary(o => o.Value);
} public IEnumerable<Instruction> ReadIL(MethodBase method)
{
MethodBody body = method.GetMethodBody();
if (body == null)
yield break; int offset = ;
byte[] il = body.GetILAsByteArray();
while (offset < il.Length)
{
int startOffset = offset;
byte opCodeByte = il[offset];
short opCodeValue = opCodeByte;
offset++; // If it's an extended opcode then grab the second byte. The 0xFE prefix codes aren't marked as prefix operators though.
if (opCodeValue == 0xFE || _opCodeList[opCodeValue].OpCodeType == OpCodeType.Prefix)
{
opCodeValue = (short)((opCodeValue << ) + il[offset]);
offset++;
} OpCode code = _opCodeList[opCodeValue]; Int64? argument = null; int argumentSize = ;
if (code.OperandType == OperandType.InlineNone)
argumentSize = ;
else if (code.OperandType == OperandType.ShortInlineBrTarget || code.OperandType == OperandType.ShortInlineI || code.OperandType == OperandType.ShortInlineVar)
argumentSize = ;
else if (code.OperandType == OperandType.InlineVar)
argumentSize = ;
else if (code.OperandType == OperandType.InlineI8 || code.OperandType == OperandType.InlineR)
argumentSize = ;
else if (code.OperandType == OperandType.InlineSwitch)
{
long num = il[offset] + (il[offset + ] << ) + (il[offset + ] << ) + (il[offset + ] << );
argumentSize = (int)( * num + );
} // This does not currently handle the 'switch' instruction meaningfully.
if (argumentSize > )
{
Int64 arg = ;
for (int i = ; i < argumentSize; ++i)
{
Int64 v = il[offset + i];
arg += v << (i * );
}
argument = arg;
offset += argumentSize;
} yield return new Instruction(startOffset, code, argument);
}
}
} public static partial class Program
{
public static void Main(string[] args)
{
var reader = new ILReader();
var module = typeof(Program).Module;
foreach (var instruction in reader.ReadIL(typeof(Program).GetMethod("Main")))
{
string arg = instruction.Argument.ToString();
if (instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldflda || instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldsflda || instruction.OpCode == OpCodes.Stfld)
arg = module.ResolveField((int)instruction.Argument).Name;
else if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Calli || instruction.OpCode == OpCodes.Callvirt)
arg = module.ResolveMethod((int)instruction.Argument).Name;
else if (instruction.OpCode == OpCodes.Newobj)
// This displays the type whose constructor is being called, but you can also determine the specific constructor and find out about its parameter types
arg = module.ResolveMethod((int)instruction.Argument).DeclaringType.FullName;
else if (instruction.OpCode == OpCodes.Ldtoken)
arg = module.ResolveMember((int)instruction.Argument).Name;
else if (instruction.OpCode == OpCodes.Ldstr)
arg = module.ResolveString((int)instruction.Argument);
else if (instruction.OpCode == OpCodes.Constrained || instruction.OpCode == OpCodes.Box)
arg = module.ResolveType((int)instruction.Argument).FullName;
else if (instruction.OpCode == OpCodes.Switch)
// For the 'switch' instruction, the "instruction.Argument" is meaningless. You'll need extra code to handle this.
arg = "?";
Console.WriteLine(instruction.OpCode + " " + arg);
}
Console.ReadLine();
}
}
}

  

根据方法名获取方法Body Content的更多相关文章

  1. VS导出方法名和方法备注的方法

    VS导出方法名和方法备注的方法 方法一: 只能导出图片格式的UML 类图 1.点击查看类图 2.在空白处点击讲关系导出为图像 方法二: 是把整个类库的方法名都罗列出来 这个方便整理一些 具体方法如下 ...

  2. Asp.net .net(C#) 获取当前命名空间,类名,方法名的方法

    public static string GetMethodInfo() {     string str = "";      //取得当前方法命名空间     str += & ...

  3. .NET C#-- 利用BeginInvoke与EndInvoke完成异步委托方法并获取方法执行返回值示例

    //定义委托 delegate string MyDelegate(string name); //定义委托调用函数 public string Hello(string name) { Thread ...

  4. localstorage和cookie的设置方法和获取方法

    1.设置localStorage window.localStorage.setItem(vm.mobileSelf,JSON.stringify(contactInfo)); vm.mobileSe ...

  5. Java学习-024-获取当前类名或方法名二三文

    今天,看朋友编写程序,打印日志时,需要记录当前类的类名以及当前方法的方法名,我发现 TA 将类名或者方法名直接写死在了代码中...虽说这样可以实现记录类名和方法名,但是当有特殊情况需要修改类名或者方法 ...

  6. java 反射机制之 getDeclaredMethod()获取方法,然后invoke执行实例对应的方法

    关于反射中getDeclaredMethod().invoke()的学习,来源于项目中的一行代码: SubjectService.class.getDeclaredMethod(autoMatchCo ...

  7. java基础:方法的定义和调用详细介绍,方法同时获取数组最大值和最小值,比较两个数组,数组交换最大最小值,附练习案列

    1. 方法概述 1.1 方法的概念 方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集 注意: 方法必须先创建才可以使用,该过程成为方法定义 方法创建后并不是直接可 ...

  8. AOP方法拦截获取参数上的注解

    https://www.jianshu.com/p/f5c7417a75f9 获取参数注解 在spring aop中,无论是前置通知的参数JoinPoint,还是环绕通知的参数ProceedingJo ...

  9. java方法句柄-----2.方法句柄的获取、变换、特殊方法句柄

    目录 1.获取方法句柄 1.1查找构造方法.一般方法和静态方法的方法句柄 1.2 查找类中的特殊方法(类中的私有方法) 1.3 查找类中静态域和一般域 1.4 通过反射API得到的Constructo ...

随机推荐

  1. stark组件之过滤操作【模仿Django的admin】

    一.先看下django的admin是如何实现过滤操作 首先在配置类中顶一个list_filter的列表,把要过滤的字段作为元素写i进去就可以了 class testbook(admin.ModelAd ...

  2. DataTable 作为ObjectDataSource的数据源

    好像是不能直接实现的.不过我从网上找到了这样的方法: 比如在BLL层中,SubjectManager是我对Subject表的业务类,此类中包含了Subject表的增删改查等各种方法.在aspx 页面中 ...

  3. call指令和ret指令配合实现子程序调用

    子程序的框架如下. 标号: 指令 ret 具有子程序的源程序的框架如下. assume cs:code code segment main: : : call sub1 : : mov ax,4c00 ...

  4. powerdesigner中实现PDM到MYSQl数据库的转换

    一.使用PowerDesigner制作建库脚本 1.设计CDM(Conceptual Data Model) 2.选择 Tools -> Generate Physical Data Model ...

  5. Linux下 MYSQL 主从复制、同步

    mysql从3.23.15版本以后提供数据库复制功能.利用该功能可以实现两个数据库同步,主从模式(A->B),互相备份模式(A<=>B)的功能. 主从模式(A->B)的配置过程 ...

  6. WebApi中Swagger的使用(超级简单)

    Swagger解释 Swagger是一种Rest API的简单但强大的表示方式,她是标准的与语言无关,这种表示方式不但人可读,而且机器可读. 可以作为Rest API的交互式文档,也可以作为Rest ...

  7. YII2表单中上传单个文件

    有些时候我们提交的表单中含有文件.怎么样让表单里的数据和文件一起提交. 我的数据表tb_user内容如下: CREATE TABLE `tb_user` ( `id` int(11) unsigned ...

  8. Oracle存储过程in、out、in out 模式参数

    Oracle存储过程in.out.in out 模式参数 标签: oracle存储inout参数 2016-11-14 11:59 3272人阅读 评论(0) 收藏 举报  分类: oracle(7) ...

  9. (转)ScriptManager.RegisterStartupScript方法和Page.ClientScript.RegisterStartupScript() 方法

    ScriptManager.RegisterStartupScript方法 如果页面中不用Ajax,cs中运行某段js代码方式可以是: Page.ClientScript.RegisterStartu ...

  10. python 打开文件对话框 filedialog tkinter GUI 编程

    - -读取文件的gui编程 # _*_ coding:utf-8 _*_ import tkinter from tkinter import filedialog def openfiles2(): ...