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

Fu7iang 2005-03-30 07:30:05

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

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

 evaELLIS 兄,我知道如何用 Emit 动态创建方法,
 可我希望得到的是方法创建后的字节码。
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}));

}

}
Fu7iang 2005-03-31
  • 打赏
  • 举报
回复

 3tzjq 兄, 我没骗人啊,
 如果能帮我解决这个问题,
 2000 分拱手奉上,绝对一分不少。
健者天行 2005-03-31
  • 打赏
  • 举报
回复
mark and UP
csdn5201 2005-03-31
  • 打赏
  • 举报
回复
不知道,关注 !
lovelxj 2005-03-31
  • 打赏
  • 举报
回复
呵呵 看起来不难
可是没有做过。。
3tzjq 2005-03-31
  • 打赏
  • 举报
回复
建议楼主不要以【2000分】的主题来诱导更旺的人气!这会让人觉得有些上当受骗的感觉。
你给20分,照样有热心的朋友来帮你解答。
hamadou 2005-03-31
  • 打赏
  • 举报
回复
up!
fgc5201314 2005-03-31
  • 打赏
  • 举报
回复
急急急..帮你up一up
conan19771130 2005-03-31
  • 打赏
  • 举报
回复
高手,帮顶
zr1982930 2005-03-31
  • 打赏
  • 举报
回复
帮顶!
LoveCherry 2005-03-31
  • 打赏
  • 举报
回复
up
top1000 2005-03-31
  • 打赏
  • 举报
回复
hehe
来接个分!
jamesfay 2005-03-31
  • 打赏
  • 举报
回复
学习学习
vickyyu 2005-03-30
  • 打赏
  • 举报
回复
up
Fu7iang 2005-03-30
  • 打赏
  • 举报
回复

 UP 者有分...
Fu7iang 2005-03-30
  • 打赏
  • 举报
回复

 倾家荡产求赐教了... :(
oyljerry 2005-03-30
  • 打赏
  • 举报
回复
楼主的分这么多?
加载更多回复(1)

110,533

社区成员

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

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

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