我正在尝试编写动态方法来复制Dictionary<,> 下面的代码抛出异常:
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Common Language Runtime detected an invalid program. at Clone(Dictionary`2 ) --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Delegate.DynamicInvokeImpl(Object[] args) at MsilTests.DynamicHelper.Clone[TKey,TValue](Dictionary`2 source) in x:\MsilTests\MsilTests\DynamicHelper.cs:line 17 at MsilTests.Program.Main(String[] args) in x:\MsilTests\MsilTests\Program.cs:line 11
当我添加以下行时会发生此异常:
generator.Emit(OpCodes.Callvirt, enumeratorType.GetMethod("MoveNext"));
我不知道为什么会抛出这个异常。 如果有人可以帮助解决这个问题,我将非常感激。
public static class DynamicHelper
{
public static Dictionary<TKey, TValue> Clone<TKey, TValue>(Dictionary<TKey, TValue> source)
{
var type = typeof (Dictionary<,>).MakeGenericType(typeof (TKey), typeof (TValue));
var genericFunc = typeof(Func<,>);
genericFunc = genericFunc.MakeGenericType(type, type);
var method = new DynamicMethod("Clone", type, new[] { type }, Assembly.GetExecutingAssembly().ManifestModule, true);
GenerateMsil(method, type);
return (Dictionary<TKey, TValue>)method.CreateDelegate(genericFunc).DynamicInvoke(source);
}
private static void GenerateMsil(DynamicMethod method, Type type)
{
var genArgs = type.GetGenericArguments();
var keyType = genArgs[0];
var valueType = genArgs[0];
var pairType = typeof(KeyValuePair<,>).MakeGenericType(keyType, valueType);
var enumeratorType = typeof(Dictionary<,>.Enumerator).MakeGenericType(keyType, valueType);
var enumerableType = typeof (IEnumerable<>).MakeGenericType(pairType);
var generator = method.GetILGenerator();
var labelRet = generator.DefineLabel();
var labelEndFinally = generator.DefineLabel();
var labelMove = generator.DefineLabel();
var labelWhile = generator.DefineLabel();
var source = generator.DeclareLocal(type); //local_0
var target = generator.DeclareLocal(type); //local_1
var enumerator = generator.DeclareLocal(enumeratorType); //local_2
var pair = generator.DeclareLocal(pairType); //local_3
var key = generator.DeclareLocal(keyType); //local_4
var value = generator.DeclareLocal(valueType); //local_5
/*Stack */
/*[0] */
/*[1] +1 */generator.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
/*[0] -1 */generator.Emit(OpCodes.Stloc, target);
/*[1] +1 */generator.Emit(OpCodes.Ldarg_0);
/*[0] -1 */generator.Emit(OpCodes.Stloc, source);
/*[1] +1 */generator.Emit(OpCodes.Ldloc, source);
/*[1] -1+1*/generator.Emit(OpCodes.Callvirt, type.GetMethod("GetEnumerator"));
/*[0] -1 */generator.Emit(OpCodes.Stloc, enumerator);
var tryFinally = generator.BeginExceptionBlock();
// try {
generator.Emit(OpCodes.Br_S, labelMove);
generator.MarkLabel(labelWhile);
/*[1] +1 */generator.Emit(OpCodes.Ldloca, enumerator);
/*[1] -1+1*/generator.Emit(OpCodes.Callvirt, enumeratorType.GetProperty("Current").GetGetMethod());
/*[0] -1 */generator.Emit(OpCodes.Stloc, pair);
/*[1] +1 */generator.Emit(OpCodes.Ldloca, pair);
/*[1] -1+1*/generator.Emit(OpCodes.Callvirt, pairType.GetProperty("Key").GetGetMethod());
/*[0] -1 */generator.Emit(OpCodes.Stloc, key);
/*[1] +1 */generator.Emit(OpCodes.Ldloca, pair);
/*[1] -1+1*/generator.Emit(OpCodes.Callvirt, pairType.GetProperty("Value").GetGetMethod());
/*[0] -1 */generator.Emit(OpCodes.Stloc, value);
/*[2] +1 */generator.Emit(OpCodes.Ldloc, target);
/*[1] +1 */generator.Emit(OpCodes.Ldloc, key);
/*[3] +1 */generator.Emit(OpCodes.Ldloc, value);
/*[0] -3 */generator.Emit(OpCodes.Callvirt, type.GetMethod("Add"));
generator.MarkLabel(labelMove);
/*[1] +1 */generator.Emit(OpCodes.Ldloc, enumerator);
/*[1] -1+1*/generator.Emit(OpCodes.Callvirt, enumeratorType.GetMethod("MoveNext"));
/*[0] -1 */generator.Emit(OpCodes.Brtrue_S, labelWhile);
generator.Emit(OpCodes.Leave_S, labelRet);
// } finally {
generator.BeginFinallyBlock();
/*[1] +1 */generator.Emit(OpCodes.Ldloca, enumerator);
/*[0] -1 */generator.Emit(OpCodes.Brfalse_S, labelEndFinally);
/*[1] +1 */generator.Emit(OpCodes.Ldloc, enumerator);
/*[0] -1 */generator.Emit(OpCodes.Callvirt, enumeratorType.GetMethod("Dispose"));
generator.MarkLabel(labelEndFinally);
generator.Emit(OpCodes.Endfinally);
// }
generator.EndExceptionBlock();
generator.MarkLabel(labelRet);
/*[1] +1 */generator.Emit(OpCodes.Ldloc, target);
/*[0] -1 */generator.Emit(OpCodes.Ret);
}
}
更新: 感谢大家的帮助和关注我的问题。 我已经解决了这个问题,它包括使用Ldloc和Dictionary&lt;,&gt; .Enumerator,但是Dictionary&lt;,&gt; .Enumerator是一个值类型,使用Ldloca将局部变量的地址传递给Call或者很重要Callvirt。 我用正确的代码更新了源代码。
答案 0 :(得分:3)
GetEnumerator
应使用CallVirt
。在底部你有:
generator.Emit(OpCodes.Ldloc, enumerator); // +1
generator.Emit(OpCodes.Callvirt, enumeratorType.GetMethod("MoveNext")); // -1+1
generator.Emit(OpCodes.Ldloc, target); // +1
generator.Emit(OpCodes.Ret); // should be 0 if void, or 1 for non-void
这是堆栈上的两个值,您尚未弹出。应该只有一个。
如果您不打算使用,请移除对MoveNext
的通话;这意味着您还需要删除之前的Ldloc
。
以下作品(但没有做任何有趣的事情):
// Create instance of Dictionaty<,>
generator.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
// Store instance in local variable "target"
generator.Emit(OpCodes.Stloc, target);
// Load first method argument to the stack (for static method - argument, for non-static - instance for with method called)
generator.Emit(OpCodes.Ldarg_0);
// Store argument in local variable "source"
generator.Emit(OpCodes.Stloc, source);
// Load value of local variable "source" to the stack
generator.Emit(OpCodes.Ldloc, source);
// Call method GetEnumerator of type IEnumerable<>
generator.Emit(OpCodes.Callvirt, type.GetMethod("GetEnumerator"));
// Store value returned from method in local variable "enumerator"
generator.Emit(OpCodes.Stloc, enumerator);
generator.Emit(OpCodes.Ldloc, target);
generator.Emit(OpCodes.Ret);
然而,它在语义上等同于:
generator.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
generator.Emit(OpCodes.Ret);
(但没有泄漏调查员)