public void GetTokenFor_StringGenerics_Success() { DynamicMethod dynamicMethod = new DynamicMethod(nameof(ContactString), typeof(string), Type.EmptyTypes, typeof(DynamicILInfoTests), false); DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo(); SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper(); sigHelper.AddArgument(typeof(MyList <string>), false); sigHelper.AddArgument(typeof(string), false); sigHelper.AddArgument(typeof(string), false); sigHelper.AddArgument(typeof(string), false); sigHelper.AddArgument(typeof(System.Collections.Generic.IEnumerator <string>), false); sigHelper.AddArgument(typeof(bool), false); dynamicILInfo.SetLocalSignature(sigHelper.GetSignature()); byte[] code = { 0x00, 0x73, 0x26, 0x00, 0x00, 0x0a, 0x0a, 0x06, 0x72, 0x29, 0x01, 0x00, 0x70, 0x6f, 0x27, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x72, 0x37, 0x01, 0x00, 0x70, 0x6f, 0x27, 0x00, 0x00, 0x0a, 0x00, 0x7e, 0x28, 0x00, 0x00, 0x0a, 0x0b, 0x00, 0x06, 0x6f, 0x29, 0x00, 0x00, 0x0a, 0x13, 0x04, 0x2b, 0x12, 0x11, 0x04, 0x6f, 0x2a, 0x00, 0x00, 0x0a, 0x0c, 0x00, 0x07, 0x08, 0x28, 0x2b, 0x00, 0x00, 0x0a, 0x0b, 0x00, 0x11, 0x04, 0x6f, 0x20, 0x00, 0x00, 0x0a, 0x13, 0x05, 0x11, 0x05, 0x2d, 0xe1, 0xde, 0x14, 0x11, 0x04, 0x14, 0xfe, 0x01, 0x13, 0x05, 0x11, 0x05, 0x2d, 0x08, 0x11, 0x04, 0x6f, 0x21, 0x00, 0x00, 0x0a, 0x00, 0xdc, 0x00, 0x07, 0x0d, 0x2b, 0x00, 0x09, 0x2a }; int token0 = dynamicILInfo.GetTokenFor(typeof(MyList <string>).GetConstructor(Type.EmptyTypes).MethodHandle, typeof(MyList <string>).TypeHandle); int token1 = dynamicILInfo.GetTokenFor("Hello~"); int token2 = dynamicILInfo.GetTokenFor(typeof(MyList <string>).GetMethod("Add").MethodHandle, typeof(MyList <string>).TypeHandle); int token3 = dynamicILInfo.GetTokenFor("World!"); int token4 = dynamicILInfo.GetTokenFor(typeof(string).GetField("Empty").FieldHandle); int token5 = dynamicILInfo.GetTokenFor(typeof(MyList <string>).GetMethod("GetEnumerator").MethodHandle, typeof(MyList <string>).TypeHandle); int token6 = dynamicILInfo.GetTokenFor(typeof(System.Collections.Generic.IEnumerator <string>).GetMethod("get_Current").MethodHandle, typeof(System.Collections.Generic.IEnumerator <string>).TypeHandle); int token7 = dynamicILInfo.GetTokenFor(typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) }).MethodHandle); int token8 = dynamicILInfo.GetTokenFor(typeof(System.Collections.IEnumerator).GetMethod("MoveNext").MethodHandle); int token9 = dynamicILInfo.GetTokenFor(typeof(System.IDisposable).GetMethod("Dispose").MethodHandle); PutInteger4(token0, 0x0002, code); PutInteger4(token1, 0x0009, code); PutInteger4(token2, 0x000e, code); PutInteger4(token3, 0x0015, code); PutInteger4(token2, 0x001a, code); PutInteger4(token4, 0x0020, code); PutInteger4(token5, 0x0028, code); PutInteger4(token6, 0x0033, code); PutInteger4(token7, 0x003c, code); PutInteger4(token8, 0x0045, code); PutInteger4(token9, 0x005f, code); dynamicILInfo.SetCode(code, 2); byte[] exceptions = { 0x41, 0x1c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x2e, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; dynamicILInfo.SetExceptions(exceptions); string ret = (string)dynamicMethod.Invoke(null, null); Assert.Equal(ret, ContactString()); }
public unsafe void SetX_NullInput_ThrowsArgumentNullException(bool skipVisibility) { DynamicMethod method = GetDynamicMethod(skipVisibility); DynamicILInfo dynamicILInfo = method.GetDynamicILInfo(); Assert.Throws <ArgumentNullException>(() => dynamicILInfo.SetCode(null, 1, 8)); Assert.Throws <ArgumentNullException>(() => dynamicILInfo.SetExceptions(null, 1)); Assert.Throws <ArgumentNullException>(() => dynamicILInfo.SetLocalSignature(null, 1)); }
// Token: 0x060000FE RID: 254 RVA: 0x0000AAF8 File Offset: 0x00008CF8 private unsafe static void c8619f80da50369e47fcfd41a1f31ff9b(ref int ca0c61e7592f10963057a6d5e207fa679, DynamicILInfo c3c57c9acc0a08dbaa5fcc9490f2a3eac) { int num = BitConverter.ToInt32(c112201421a408a8f2963cee12a5d2e69.c271a832e629a79991d9eda7a270cb125, ca0c61e7592f10963057a6d5e207fa679); ca0c61e7592f10963057a6d5e207fa679 += 4; if (num == 0) { for (;;) { switch (2) { case 0: continue; } break; } if (!true) { RuntimeMethodHandle runtimeMethodHandle = methodof(c112201421a408a8f2963cee12a5d2e69.c8619f80da50369e47fcfd41a1f31ff9b(int *, DynamicILInfo)).MethodHandle; } return; } byte[] array = c4cbbd90ed4559089f2970be8fc52599d.cad714a49573fd585a8ab14c80a23536b(num); Buffer.BlockCopy(c112201421a408a8f2963cee12a5d2e69.c271a832e629a79991d9eda7a270cb125, ca0c61e7592f10963057a6d5e207fa679, array, 0, num); int num2 = 4; int num3 = (num - 4) / 24; for (int i = 0; i < num3; i++) { ExceptionHandlingClauseOptions exceptionHandlingClauseOptions = (ExceptionHandlingClauseOptions)BitConverter.ToInt32(array, num2); num2 += 20; switch (exceptionHandlingClauseOptions) { case ExceptionHandlingClauseOptions.Clause: { RuntimeTypeHandle type = c112201421a408a8f2963cee12a5d2e69.c5b2cbb035f512aa824f5593dd0096224.ResolveTypeHandle(BitConverter.ToInt32(array, num2)); int tokenFor = c3c57c9acc0a08dbaa5fcc9490f2a3eac.GetTokenFor(type); c112201421a408a8f2963cee12a5d2e69.c080978af586a672599ee810498652c16(tokenFor, num2, array); break; } case ExceptionHandlingClauseOptions.Fault: throw new NotSupportedException("dynamic method does not support fault clause"); } num2 += 4; } for (;;) { switch (2) { case 0: continue; } break; } c3c57c9acc0a08dbaa5fcc9490f2a3eac.SetExceptions(array); }
private static void SetExceptions(MethodBody body, DynamicILInfo ilInfo) { IList <ExceptionHandlingClause> ehcs = body.ExceptionHandlingClauses; int ehCount = ehcs.Count; if (ehCount == 0) { return; } // Let us do FAT exception header int size = 4 + 24 * ehCount; byte[] exceptions = new byte[size]; exceptions[0] = 0x01 | 0x40; //Offset: 0, Kind: CorILMethod_Sect_EHTable | CorILMethod_Sect_FatFormat OverwriteInt32(size, 1, exceptions); // Offset: 1, DataSize: n * 24 + 4 int pos = 4; foreach (ExceptionHandlingClause ehc in ehcs) { // // Flags, TryOffset, TryLength, HandlerOffset, HandlerLength, // OverwriteInt32((int)ehc.Flags, pos, exceptions); pos += 4; OverwriteInt32(ehc.TryOffset, pos, exceptions); pos += 4; OverwriteInt32(ehc.TryLength, pos, exceptions); pos += 4; OverwriteInt32(ehc.HandlerOffset, pos, exceptions); pos += 4; OverwriteInt32(ehc.HandlerLength, pos, exceptions); pos += 4; // // ClassToken or FilterOffset // switch (ehc.Flags) { case ExceptionHandlingClauseOptions.Clause: int token = ilInfo.GetTokenFor(ehc.CatchType.TypeHandle); OverwriteInt32(token, pos, exceptions); break; case ExceptionHandlingClauseOptions.Filter: OverwriteInt32(ehc.FilterOffset, pos, exceptions); break; case ExceptionHandlingClauseOptions.Fault: throw new NotSupportedException("dynamic method does not support fault clause"); case ExceptionHandlingClauseOptions.Finally: break; } pos += 4; } ilInfo.SetExceptions(exceptions); }
public void GetTokenFor_Exception_Success() { DynamicMethod dynamicMethod = new DynamicMethod(nameof(ExceptionTest), typeof(int), Type.EmptyTypes, typeof(DynamicILInfoTests), false); DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo(); SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper(); sigHelper.AddArgument(typeof(int), false); sigHelper.AddArgument(typeof(int), false); sigHelper.AddArgument(typeof(int), false); sigHelper.AddArgument(typeof(string), false); sigHelper.AddArgument(typeof(int), false); dynamicILInfo.SetLocalSignature(sigHelper.GetSignature()); byte[] code = { 0x00, 0x16, 0x0a, 0x00, 0x00, 0x16, 0x0b, 0x17, 0x07, 0x5b, 0x0c, 0x00, 0xde, 0x09, 0x26, 0x00, 0x06, 0x17, 0x58, 0x0a, 0x00, 0xde, 0x00, 0x00, 0x00, 0x72, 0xed, 0x01, 0x00, 0x70, 0x17, 0x28, 0x32, 0x00, 0x00, 0x0a, 0x26, 0x00, 0xde, 0x09, 0x26, 0x00, 0x06, 0x17, 0x58, 0x0a, 0x00, 0xde, 0x00, 0x00, 0x14, 0x0d, 0x09, 0x6f, 0x05, 0x00, 0x00, 0x0a, 0x26, 0x00, 0xde, 0x09, 0x26, 0x00, 0x06, 0x17, 0x58, 0x0a, 0x00, 0xde, 0x00, 0x00, 0xde, 0x07, 0x00, 0x06, 0x18, 0x58, 0x0a, 0x00, 0xdc, 0x00, 0x06, 0x13, 0x04, 0x2b, 0x00, 0x11, 0x04, 0x2a }; int token0 = dynamicILInfo.GetTokenFor("A.B"); int token1 = dynamicILInfo.GetTokenFor(typeof(System.Type).GetMethod("GetType", new Type[] { typeof(string), typeof(bool) }).MethodHandle); int token2 = dynamicILInfo.GetTokenFor(typeof(string).GetMethod("ToUpper", Type.EmptyTypes).MethodHandle); PutInteger4(token0, 0x001a, code); PutInteger4(token1, 0x0020, code); PutInteger4(token2, 0x0036, code); dynamicILInfo.SetCode(code, 2); int token3 = dynamicILInfo.GetTokenFor(typeof(System.Object).TypeHandle); int token4 = dynamicILInfo.GetTokenFor(typeof(System.Exception).TypeHandle); int token5 = dynamicILInfo.GetTokenFor(typeof(System.NullReferenceException).TypeHandle); byte[] exceptions = { 0x41, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x47, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; PutInteger4(token3, 0x0018, exceptions); PutInteger4(token4, 0x0030, exceptions); PutInteger4(token5, 0x0048, exceptions); dynamicILInfo.SetExceptions(exceptions); int ret = (int)dynamicMethod.Invoke(null, null); Assert.Equal(ret, ExceptionTest()); }
public void GetTokenFor_IntGenerics_Success() { DynamicMethod dynamicMethod = new DynamicMethod(nameof(SumInteger), typeof(int), new Type[] { }, typeof(DynamicILInfoTests), false); DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo(); SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper(); sigHelper.AddArgument(typeof(MyList <int>), false); sigHelper.AddArgument(typeof(int), false); sigHelper.AddArgument(typeof(int), false); sigHelper.AddArgument(typeof(int), false); sigHelper.AddArgument(typeof(System.Collections.Generic.IEnumerator <int>), false); sigHelper.AddArgument(typeof(bool), false); dynamicILInfo.SetLocalSignature(sigHelper.GetSignature()); byte[] code = { 0x00, 0x73, 0x1c, 0x00, 0x00, 0x0a, 0x0a, 0x06, 0x1f, 0x64, 0x6f, 0x1d, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x20, 0xc8, 0x00, 0x00, 0x00, 0x6f, 0x1d, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x20, 0x2c, 0x01, 0x00, 0x00, 0x6f, 0x1d, 0x00, 0x00, 0x0a, 0x00, 0x16, 0x0b, 0x00, 0x06, 0x6f, 0x1e, 0x00, 0x00, 0x0a, 0x13, 0x04, 0x2b, 0x0e, 0x11, 0x04, 0x6f, 0x1f, 0x00, 0x00, 0x0a, 0x0c, 0x00, 0x07, 0x08, 0x58, 0x0b, 0x00, 0x11, 0x04, 0x6f, 0x20, 0x00, 0x00, 0x0a, 0x13, 0x05, 0x11, 0x05, 0x2d, 0xe5, 0xde, 0x14, 0x11, 0x04, 0x14, 0xfe, 0x01, 0x13, 0x05, 0x11, 0x05, 0x2d, 0x08, 0x11, 0x04, 0x6f, 0x21, 0x00, 0x00, 0x0a, 0x00, 0xdc, 0x00, 0x07, 0x0d, 0x2b, 0x00, 0x09, 0x2a }; int token0 = dynamicILInfo.GetTokenFor(typeof(MyList <int>).GetConstructors()[0].MethodHandle, typeof(MyList <int>).TypeHandle); int token1 = dynamicILInfo.GetTokenFor(typeof(MyList <int>).GetMethod("Add").MethodHandle, typeof(MyList <int>).TypeHandle); int token2 = dynamicILInfo.GetTokenFor(typeof(MyList <int>).GetMethod("GetEnumerator").MethodHandle, typeof(MyList <int>).TypeHandle); int token3 = dynamicILInfo.GetTokenFor(typeof(System.Collections.Generic.IEnumerator <int>).GetMethod("get_Current").MethodHandle, typeof(System.Collections.Generic.IEnumerator <int>).TypeHandle); int token4 = dynamicILInfo.GetTokenFor(typeof(System.Collections.IEnumerator).GetMethod("MoveNext").MethodHandle); int token5 = dynamicILInfo.GetTokenFor(typeof(System.IDisposable).GetMethod("Dispose").MethodHandle); PutInteger4(token0, 0x0002, code); PutInteger4(token1, 0x000b, code); PutInteger4(token1, 0x0017, code); PutInteger4(token1, 0x0023, code); PutInteger4(token2, 0x002d, code); PutInteger4(token3, 0x0038, code); PutInteger4(token4, 0x0046, code); PutInteger4(token5, 0x0060, code); dynamicILInfo.SetCode(code, 2); byte[] exceptions = { 0x41, 0x1c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; dynamicILInfo.SetExceptions(exceptions); int ret = (int)dynamicMethod.Invoke(null, null); Assert.Equal(ret, SumInteger()); }
/// <summary> /// Binds this method body to a method dynamically generated with the DynamicMethod /// API. /// </summary> /// <param name="dynamicILInfo">The <see cref="DynamicILInfo"/> for the dynamic method.</param> public void bindToDynamicILInfo(DynamicILInfo dynamicILInfo) { dynamicILInfo.SetCode(m_methodBody, m_maxStack); // DynamicILInfo requires a valid local signature, so use the empty signature // if there are no locals. dynamicILInfo.SetLocalSignature((m_localSignature.Length != 0) ? m_localSignature : s_emptyLocalSig); if (m_ehSection.Length != 0) { dynamicILInfo.SetExceptions(m_ehSection); } }
public unsafe void SetX_NegativeInputSize_ThrowsArgumentOutOfRangeException(bool skipVisibility) { DynamicMethod method = GetDynamicMethod(skipVisibility); DynamicILInfo dynamicILInfo = method.GetDynamicILInfo(); var bytes = new byte[] { 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02 }; Assert.Throws <ArgumentOutOfRangeException>(() => { fixed(byte *bytesPtr = bytes) { dynamicILInfo.SetCode(bytesPtr, -1, 8); } }); Assert.Throws <ArgumentOutOfRangeException>(() => { fixed(byte *bytesPtr = bytes) { dynamicILInfo.SetExceptions(bytesPtr, -1); } }); Assert.Throws <ArgumentOutOfRangeException>(() => { fixed(byte *bytesPtr = bytes) { dynamicILInfo.SetLocalSignature(bytesPtr, -1); } }); }