public void GetTokenFor_DynamicMethod () { DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(RuntimeMethodHandle), Type.EmptyTypes, typeof(DynamicILInfoTest), false); DynamicILInfo il = dm.GetDynamicILInfo(); byte[] code = { 0x00, 0xd0, 0x01, 0x00, 0x00, 0x70, 0x2a }; int token0 = il.GetTokenFor(dm); PutInteger4(token0, 0x0002, code); il.SetCode(code, 8); var res = dm.Invoke(null, null); Assert.AreEqual (dm.MethodHandle, res); }
public void GetTokenFor_String () { DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(string), Type.EmptyTypes, typeof(DynamicILInfoTest), false); DynamicILInfo il = dm.GetDynamicILInfo(); byte[] code = { 0x00, 0x72, 0x01, 0x00, 0x00, 0x70, 0x2a }; int token0 = il.GetTokenFor("ABCD"); PutInteger4(token0, 0x0002, code); il.SetCode(code, 8); var res = dm.Invoke(null, null); Assert.AreEqual ("ABCD", res); }
public void GetTokenFor_Type () { DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(Type), Type.EmptyTypes, typeof(DynamicILInfoTest), false); DynamicILInfo il = dm.GetDynamicILInfo(); byte[] code = { 0x00, 0xd0, 0x01, 0x00, 0x00, 0x70, 0x28, 0x04, 0x00, 0x00, 0x0a, 0x00, 0x2a }; int token0 = il.GetTokenFor(typeof (int).TypeHandle); int token1 = il.GetTokenFor(typeof(Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) }).MethodHandle); PutInteger4(token0, 0x0002, code); PutInteger4(token1, 0x0007, code); il.SetCode(code, 8); var res = dm.Invoke(null, null); Assert.AreEqual (typeof (int), res); }
public void GetTokenFor_Method () { DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(string), Type.EmptyTypes, typeof(DynamicILInfoTest), false); DynamicILInfo il = dm.GetDynamicILInfo(); // ldstr "ABCD"; call string::ToLower (); ret byte[] code = { 0x00, 0x72, 0x01, 0x00, 0x00, 0x70, 0x28, 0x04, 0x00, 0x00, 0x0a, 0x00, 0x2a }; int token0 = il.GetTokenFor("ABCD"); int token1 = il.GetTokenFor(typeof(string).GetMethod("ToLower", Type.EmptyTypes).MethodHandle); PutInteger4(token0, 0x0002, code); PutInteger4(token1, 0x0007, code); il.SetCode(code, 8); var res = dm.Invoke(null, null); Assert.AreEqual ("abcd", res); }
public void GetTokenFor_FieldInfo () { aField = aField + 1; DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(RuntimeFieldHandle), Type.EmptyTypes, typeof(DynamicILInfoTest), false); DynamicILInfo il = dm.GetDynamicILInfo(); var f = typeof (DynamicILInfoTest).GetField ("aField", BindingFlags.Static|BindingFlags.NonPublic).FieldHandle; byte[] code = { 0x00, 0xd0, 0x01, 0x00, 0x00, 0x70, 0x2a }; int token0 = il.GetTokenFor(f); PutInteger4(token0, 0x0002, code); il.SetCode(code, 8); var res = dm.Invoke(null, null); Assert.AreEqual (f, res); }
public void GetDynamicMethod () { DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(string), Type.EmptyTypes, typeof(DynamicILInfoTest), false); DynamicILInfo il = dm.GetDynamicILInfo(); Assert.AreEqual (dm, il.DynamicMethod); }
public void GetDynamicILInfo_Unique () { DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(string), Type.EmptyTypes, typeof(DynamicILInfoTest), false); DynamicILInfo il = dm.GetDynamicILInfo(); DynamicILInfo il2 = dm.GetDynamicILInfo(); Assert.IsTrue (Object.ReferenceEquals (il, il2)); }
//------------------------------------------------------------------------------------- /// <summary> /// Выполняет кодовый запрос на объекте. /// </summary> /// <param name="dstObject">Объект, на котором будет выполнен запрос.</param> /// <param name="query">Выполняемый кодовый запрос.</param> public static object ExecCodeQuery(object dstObject, CodeQuery query) { try { DynamicMethod dm = new DynamicMethod("DynMethod", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(object), new Type[] { dstObject.GetType() }, dstObject.GetType(), true); //DynamicMethod dm = new DynamicMethod("DynMethod", null, null, dstObject.GetType(), true); DynamicILInfo dii = dm.GetDynamicILInfo(); SignatureHelper sig = SignatureHelper.GetLocalVarSigHelper(); if(query.LocalVariables != null) foreach(KeyValuePair<Type, bool> i in query.LocalVariables) sig.AddArgument(i.Key, i.Value); dii.SetLocalSignature(sig.GetSignature()); Customize(query.Body, dii, query.Schema); dii.SetCode(query.Body, query.MaxStackSize); DynMethod deleg = (DynMethod)dm.CreateDelegate(typeof(DynMethod), dstObject); return deleg(); //dm.Invoke(dstObject, new object[] { dstObject }); } catch { throw; } }
[Test] // bug #13969 public void GetTokenFor_Constructor () { var m = typeof (object).GetConstructor (Type.EmptyTypes); var dm = new DynamicMethod ("Foo", typeof (void), Type.EmptyTypes); var dil = dm.GetDynamicILInfo (); dil.GetTokenFor (m.MethodHandle); }