(首先,这是一篇很长的文章,但不用担心:我已经实现了所有这一切,我只是在问你的意见或可能的替代方案。)替换方法的BodyBody中的指令
我在执行以下操作时遇到问题;我会感谢一些帮助:
- 我得到一个
Type
作为参数。 - 我使用反射定义了一个子类。请注意,我不打算修改原始类型,但创建一个新类型。
创建每原始类,的字段的属性,像这样:
public class OriginalClass { private int x; } public class Subclass : OriginalClass { private int x; public int X { get { return x; } set { x = value; } } }
对于超类的每一个方法,创建在子类中的类似方法。除了我用
callvirt this.get_X
替换指令ldfld x
之外,该方法的主体必须相同,也就是说,不是直接从字段中读取,而是调用get访问器。
我在第4步遇到问题。我知道你不应该操纵这样的代码,但我真的需要。
这是我已经试过:
尝试#1:使用Mono.Cecil能做到。这将允许我将该方法的主体解析为可读的Instructions
,并轻松地替换说明。但是,原始类型不在.dll文件中,所以我找不到用Mono.Cecil加载它的方法。将类型写入.dll,然后加载它,然后修改它并将新类型写入磁盘(我认为这是您使用Mono.Cecil创建类型的方式),然后加载它似乎是一个巨大的开销。
尝试#2:使用Mono.Reflection。这也可以让我解析身体到Instructions
,但是我不支持替换说明。我已经使用Mono.Reflection实现了一个非常丑陋和低效的解决方案,但它还不支持包含try-catch语句的方法(尽管我想我可以实现这一点),并且我担心可能会有其他场景它不会工作,因为我以一种不寻常的方式使用ILGenerator
。此外,这是非常丑陋的;)。下面是我做了什么:
private void TransformMethod(MethodInfo methodInfo) {
// Create a method with the same signature.
ParameterInfo[] paramList = methodInfo.GetParameters();
Type[] args = new Type[paramList.Length];
for (int i = 0; i < args.Length; i++) {
args[i] = paramList[i].ParameterType;
}
MethodBuilder methodBuilder = typeBuilder.DefineMethod(
methodInfo.Name, methodInfo.Attributes, methodInfo.ReturnType, args);
ILGenerator ilGen = methodBuilder.GetILGenerator();
// Declare the same local variables as in the original method.
IList<LocalVariableInfo> locals = methodInfo.GetMethodBody().LocalVariables;
foreach (LocalVariableInfo local in locals) {
ilGen.DeclareLocal(local.LocalType);
}
// Get readable instructions.
IList<Instruction> instructions = methodInfo.GetInstructions();
// I first need to define labels for every instruction in case I
// later find a jump to that instruction. Once the instruction has
// been emitted I cannot label it, so I'll need to do it in advance.
// Since I'm doing a first pass on the method's body anyway, I could
// instead just create labels where they are truly needed, but for
// now I'm using this quick fix.
Dictionary<int, Label> labels = new Dictionary<int, Label>();
foreach (Instruction instr in instructions) {
labels[instr.Offset] = ilGen.DefineLabel();
}
foreach (Instruction instr in instructions) {
// Mark this instruction with a label, in case there's a branch
// instruction that jumps here.
ilGen.MarkLabel(labels[instr.Offset]);
// If this is the instruction that I want to replace (ldfld x)...
if (instr.OpCode == OpCodes.Ldfld) {
// ...get the get accessor for the accessed field (get_X())
// (I have the accessors in a dictionary; this isn't relevant),
MethodInfo safeReadAccessor = dataMembersSafeAccessors[((FieldInfo) instr.Operand).Name][0];
// ...instead of emitting the original instruction (ldfld x),
// emit a call to the get accessor,
ilGen.Emit(OpCodes.Callvirt, safeReadAccessor);
// Else (it's any other instruction), reemit the instruction, unaltered.
} else {
Reemit(instr, ilGen, labels);
}
}
}
这里来的可怕,可怕的Reemit
方法:
private void Reemit(Instruction instr, ILGenerator ilGen, Dictionary<int, Label> labels) {
// If the instruction doesn't have an operand, emit the opcode and return.
if (instr.Operand == null) {
ilGen.Emit(instr.OpCode);
return;
}
// Else (it has an operand)...
// If it's a branch instruction, retrieve the corresponding label (to
// which we want to jump), emit the instruction and return.
if (instr.OpCode.FlowControl == FlowControl.Branch) {
ilGen.Emit(instr.OpCode, labels[Int32.Parse(instr.Operand.ToString())]);
return;
}
// Otherwise, simply emit the instruction. I need to use the right
// Emit call, so I need to cast the operand to its type.
Type operandType = instr.Operand.GetType();
if (typeof(byte).IsAssignableFrom(operandType))
ilGen.Emit(instr.OpCode, (byte) instr.Operand);
else if (typeof(double).IsAssignableFrom(operandType))
ilGen.Emit(instr.OpCode, (double) instr.Operand);
else if (typeof(float).IsAssignableFrom(operandType))
ilGen.Emit(instr.OpCode, (float) instr.Operand);
else if (typeof(int).IsAssignableFrom(operandType))
ilGen.Emit(instr.OpCode, (int) instr.Operand);
... // you get the idea. This is a pretty long method, all like this.
}
分支指令是一个特殊情况,因为instr.Operand
是SByte
,但Emit
预计Label
类型的操作数。因此需要Dictionary labels
。
正如你所看到的,这是非常可怕的。更重要的是,它在所有情况下都不起作用,例如包含try-catch语句的方法,因为我没有使用方法BeginExceptionBlock
,BeginCatchBlock
等ILGenerator
发出它们。这变得越来越复杂。我想我可以做到这一点:MethodBody
有一个ExceptionHandlingClause
列表,其中应包含必要的信息来做到这一点。但我不喜欢这个解决方案,所以我会把它作为最后的解决方案。
尝试3:去裸回来,只是复制由MethodBody.GetILAsByteArray()
返回的字节数组,因为我只是想更换单个指令产生完全相同的结果同样大小的另一个单指令:它加载堆栈中相同类型的对象等等。所以不会有任何标签转移,并且所有东西都应该完全相同。我已经完成了这个工作,取代了数组的特定字节,然后调用MethodBuilder.CreateMethodBody(byte[], int)
,但我仍然遇到与异常相同的错误,并且仍然需要声明局部变量,否则我会得到一个错误...即使当我复制方法的主体,不要改变任何东西。 所以这是更高效,但我仍然要照顾的例外等
感叹。
这里是尝试#3的执行情况,如果有人有兴趣:(。我知道这是不是很抱歉,我把它赶紧起来看看它是否会工作)
private void TransformMethod(MethodInfo methodInfo, Dictionary<string, MethodInfo[]> dataMembersSafeAccessors, ModuleBuilder moduleBuilder) {
ParameterInfo[] paramList = methodInfo.GetParameters();
Type[] args = new Type[paramList.Length];
for (int i = 0; i < args.Length; i++) {
args[i] = paramList[i].ParameterType;
}
MethodBuilder methodBuilder = typeBuilder.DefineMethod(
methodInfo.Name, methodInfo.Attributes, methodInfo.ReturnType, args);
ILGenerator ilGen = methodBuilder.GetILGenerator();
IList<LocalVariableInfo> locals = methodInfo.GetMethodBody().LocalVariables;
foreach (LocalVariableInfo local in locals) {
ilGen.DeclareLocal(local.LocalType);
}
byte[] rawInstructions = methodInfo.GetMethodBody().GetILAsByteArray();
IList<Instruction> instructions = methodInfo.GetInstructions();
int k = 0;
foreach (Instruction instr in instructions) {
if (instr.OpCode == OpCodes.Ldfld) {
MethodInfo safeReadAccessor = dataMembersSafeAccessors[((FieldInfo) instr.Operand).Name][0];
// Copy the opcode: Callvirt.
byte[] bytes = toByteArray(OpCodes.Callvirt.Value);
for (int m = 0; m < OpCodes.Callvirt.Size; m++) {
rawInstructions[k++] = bytes[put.Length - 1 - m];
}
// Copy the operand: the accessor's metadata token.
bytes = toByteArray(moduleBuilder.GetMethodToken(safeReadAccessor).Token);
for (int m = instr.Size - OpCodes.Ldfld.Size - 1; m >= 0; m--) {
rawInstructions[k++] = bytes[m];
}
// Skip this instruction (do not replace it).
} else {
k += instr.Size;
}
}
methodBuilder.CreateMethodBody(rawInstructions, rawInstructions.Length);
}
private static byte[] toByteArray(int intValue) {
byte[] intBytes = BitConverter.GetBytes(intValue);
if (BitConverter.IsLittleEndian)
Array.Reverse(intBytes);
return intBytes;
}
private static byte[] toByteArray(short shortValue) {
byte[] intBytes = BitConverter.GetBytes(shortValue);
if (BitConverter.IsLittleEndian)
Array.Reverse(intBytes);
return intBytes;
}
我没有多少希望,但任何人都可以提出比这更好的建议吗?
很抱歉,这篇帖子非常冗长,谢谢。
更新#1: Aggh ......我刚读这in the msdn documentation:
[该CreateMethodBody方法]是目前 不完全支持。 用户无法提供 令牌修复程序和异常处理程序的位置。
我应该在尝试任何事情之前阅读文档。有一天我会学习......
这意味着选项#3不能支持try-catch语句,这对我来说是无用的。我真的必须使用可怕的#2吗? :/ 帮帮我! :P
更新#2:我已经成功地实现尝试#2与异常的支持。这非常丑陋,但它很有用。当我细化代码时,我会在这里发布它。这不是一个优先事项,所以从现在起可能需要几个星期。只要有人对此感兴趣就让你知道。
感谢您的建议。
你说你已经用第二种方法解决了这个问题。你可以在这里发布你的解决方案(例如链接到源代码)。提前致谢! – 2011-07-06 08:43:53
是啊,将不胜感激,你是否真的设法取代旧的方法与新的?或者你是否刚刚创建了一个具有不同行为的新动态方法,并将其包装到代理类中? – 2013-04-18 07:07:29