public void CreateDelegate_InvalidTarget_ThrowsArgumentException() { FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance); DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass)); ILGenerator ilGenerator = method.GetILGenerator(); Helpers.EmitMethodBody(ilGenerator, field); AssertExtensions.Throws <ArgumentException>(null, () => method.CreateDelegate(typeof(IntDelegate), "foo")); }
public void CreateDelegate_DelegateTypeInvalid_ThrowsArgumentException(Type delegateType) { FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance); DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass)); ILGenerator ilGenerator = method.GetILGenerator(); Helpers.EmitMethodBody(ilGenerator, field); Assert.Throws <ArgumentException>(null, () => method.CreateDelegate(delegateType)); Assert.Throws <ArgumentException>(null, () => method.CreateDelegate(delegateType, new IDClass())); }
public void CreateDelegate_Type(IDClass target) { int newId = 0; FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance); DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass)); ILGenerator ilGenerator = method.GetILGenerator(); Helpers.EmitMethodBody(ilGenerator, field); IDClassDelegate staticCallBack = (IDClassDelegate)method.CreateDelegate(typeof(IDClassDelegate)); Assert.Equal(staticCallBack(target, newId), target.ID); Assert.Equal(newId, target.ID); }
public void GetILGenerator_Int_Owner(bool skipVisibility) { IDClass target = new IDClass(); FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.Instance | BindingFlags.NonPublic); Type[] paramTypes = new Type[] { typeof(IDClass), typeof(int) }; DynamicMethod method = new DynamicMethod("Method", typeof(int), paramTypes, typeof(IDClass), skipVisibility); ILGenerator ilGenerator = method.GetILGenerator(8); Helpers.EmitMethodBody(ilGenerator, field); IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target); VerifyILGenerator(instanceCallBack, target, 0); }
public void CreateDelegate_Target_Module(IDClass target) { Module module = typeof(TestClass).GetTypeInfo().Module; int newId = 0; FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance); DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, module, true); ILGenerator ilGenerator = method.GetILGenerator(); Helpers.EmitMethodBody(ilGenerator, field); IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target); Assert.Equal(instanceCallBack(newId), target.ID); Assert.Equal(newId, target.ID); }