public void NegTest1() { DynamicMethod testDynMethod; TestCreateDelegateOwner2 target = new TestCreateDelegateOwner2(); testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE); Assert.Throws <InvalidOperationException>(() => { UseLikeStatic2 staticCallBack = (UseLikeStatic2)testDynMethod.CreateDelegate(typeof(UseLikeStatic2)); }); }
public void PosTest2() { DynamicMethod testDynMethod; FieldInfo fieldInfo; TestCreateDelegateOwner2Derived target = new TestCreateDelegateOwner2Derived(); int newId = 100; bool actualResult; fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( c_FIELD_NAME, BindingFlags.NonPublic | BindingFlags.Instance); testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE); this.EmitDynMethodBody(testDynMethod, fieldInfo); UseLikeStatic2 staticCallBack = (UseLikeStatic2)testDynMethod.CreateDelegate(typeof(UseLikeStatic2)); actualResult = target.ID == staticCallBack(target, newId); actualResult = (target.ID == newId) && actualResult; Assert.True(actualResult, "Failed to create delegate for dynamic method."); }