/// <summary> /// Load instance argument /// </summary> /// <param name="il">LazyILGenerator instance</param> /// <param name="actualType">Actual type</param> /// <param name="expectedType">Expected type</param> internal static void LoadInstanceArgument(this LazyILGenerator il, Type actualType, Type expectedType) { il.Emit(OpCodes.Ldarg_0); if (actualType == expectedType) { return; } if (expectedType.IsValueType) { il.DeclareLocal(expectedType); il.Emit(OpCodes.Unbox_Any, expectedType); il.Emit(OpCodes.Stloc_0); il.Emit(OpCodes.Ldloca_S, 0); } else { il.Emit(OpCodes.Castclass, expectedType); } }