【2000分】【在线等待】【急急急】如何获取方法体的字节码?

Fu7iang 2005-03-30 07:29:59

 在 .Net 中用 Emit 动态创建方法很容易,
 可我想获取一个方法体的字节码,应该怎么做?
...全文
152 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
tylike 2005-03-31
  • 打赏
  • 举报
回复
:)
Fu7iang 2005-03-31
  • 打赏
  • 举报
回复

 问题已经解决了,谢谢楼上的兄弟们捧场,晚上来散分。
jialiang 2005-03-31
  • 打赏
  • 举报
回复
up
evaELLIS 2005-03-31
  • 打赏
  • 举报
回复


using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

class DynamicJumpTableDemo

{

public static Type BuildMyType()
{
AppDomain myDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "MyDynamicAssembly";

AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
myAsmName,
AssemblyBuilderAccess.Run);
ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule(
"MyJumpTableDemo");

TypeBuilder myTypeBuilder = myModBuilder.DefineType("JumpTableDemo",
TypeAttributes.Public);
MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("SwitchMe",
MethodAttributes.Public |
MethodAttributes.Static,
typeof(string),
new Type[] {typeof(int)});

ILGenerator myIL = myMthdBuilder.GetILGenerator();

Label defaultCase = myIL.DefineLabel();
Label endOfMethod = myIL.DefineLabel();

// We are initializing our jump table. Note that the labels
// will be placed later using the MarkLabel method.

Label[] jumpTable = new Label[] { myIL.DefineLabel(),
myIL.DefineLabel(),
myIL.DefineLabel(),
myIL.DefineLabel(),
myIL.DefineLabel() };

// arg0, the number we passed, is pushed onto the stack.
// In this case, due to the design of the code sample,
// the value pushed onto the stack happens to match the
// index of the label (in IL terms, the index of the offset
// in the jump table). If this is not the case, such as
// when switching based on non-integer values, rules for the correspondence
// between the possible case values and each index of the offsets
// must be established outside of the ILGenerator.Emit calls,
// much as a compiler would.

myIL.Emit(OpCodes.Ldarg_0);
myIL.Emit(OpCodes.Switch, jumpTable);

// Branch on default case
myIL.Emit(OpCodes.Br_S, defaultCase);

// Case arg0 = 0
myIL.MarkLabel(jumpTable[0]);
myIL.Emit(OpCodes.Ldstr, "are no bananas");
myIL.Emit(OpCodes.Br_S, endOfMethod);

// Case arg0 = 1
myIL.MarkLabel(jumpTable[1]);
myIL.Emit(OpCodes.Ldstr, "is one banana");
myIL.Emit(OpCodes.Br_S, endOfMethod);

// Case arg0 = 2
myIL.MarkLabel(jumpTable[2]);
myIL.Emit(OpCodes.Ldstr, "are two bananas");
myIL.Emit(OpCodes.Br_S, endOfMethod);

// Case arg0 = 3
myIL.MarkLabel(jumpTable[3]);
myIL.Emit(OpCodes.Ldstr, "are three bananas");
myIL.Emit(OpCodes.Br_S, endOfMethod);

// Case arg0 = 4
myIL.MarkLabel(jumpTable[4]);
myIL.Emit(OpCodes.Ldstr, "are four bananas");
myIL.Emit(OpCodes.Br_S, endOfMethod);

// Default case
myIL.MarkLabel(defaultCase);
myIL.Emit(OpCodes.Ldstr, "are many bananas");

myIL.MarkLabel(endOfMethod);
myIL.Emit(OpCodes.Ret);

return myTypeBuilder.CreateType();

}

public static void Main()
{
Type myType = BuildMyType();

Console.Write("Enter an integer between 0 and 5: ");
int theValue = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("---");
Object myInstance = Activator.CreateInstance(myType, new object[0]);
Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe",
BindingFlags.InvokeMethod,
null,
myInstance,
new object[] {theValue}));

}

}
qozms 2005-03-31
  • 打赏
  • 举报
回复
UP 一下
Fu7iang 2005-03-31
  • 打赏
  • 举报
回复

 UP 一下
conan19771130 2005-03-31
  • 打赏
  • 举报
回复
高手,帮顶
zr1982930 2005-03-31
  • 打赏
  • 举报
回复
帮顶!
LoveCherry 2005-03-31
  • 打赏
  • 举报
回复
up
egxsun 2005-03-31
  • 打赏
  • 举报
回复
jf
timiil 2005-03-31
  • 打赏
  • 举报
回复
gx
aqiang1979 2005-03-31
  • 打赏
  • 举报
回复
看看
dtfox 2005-03-31
  • 打赏
  • 举报
回复
mark
huadiexiaowu 2005-03-31
  • 打赏
  • 举报
回复
接分~~
wokagoka 2005-03-31
  • 打赏
  • 举报
回复
接分
top1000 2005-03-31
  • 打赏
  • 举报
回复
hehe
来接个分!
jamesfay 2005-03-31
  • 打赏
  • 举报
回复
学习学习
syeerzy 2005-03-31
  • 打赏
  • 举报
回复
:)
1000分啊。。。。我数了下,刚才楼主一共发了10贴一样的问题
hedonister 2005-03-30
  • 打赏
  • 举报
回复
无能为力,楼主我没办法帮你,实在太难了,关注一下,解决了一定拿来交流交流
qianduo 2005-03-30
  • 打赏
  • 举报
回复
UP.
没用过,学习
加载更多回复(3)
YOLO高设计资源源码,详情请查看资源内容中使用说明 YOLO高设计资源源码,详情请查看资源内容中使用说明 YOLO高设计资源源码,详情请查看资源内容中使用说明 YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明YOLO高设计资源源码,详情请查看资源内容中使用说明

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧