public void CallInsntaceMethods() { var mAct = typeof(InnerClass).GetMethod("Act"); var act = MethodInvokerGenerator.CreateDelegate(mAct); var instance = new InnerClass(); var result = act(instance, new object[] { 123 }); Assert.IsNull(result); Assert.AreEqual(123, instance.Value); result = act(instance, new object[] { 123, "argument to be ignored" }); Assert.IsNull(result); Assert.AreEqual(123, instance.Value); var mIntFunc = typeof(InnerClass).GetMethod("IntFunc", BindingFlags.Instance | BindingFlags.NonPublic); var intFunc = MethodInvokerGenerator.CreateDelegate(mIntFunc); result = intFunc(instance, null); Assert.AreEqual(1, result); var mStrFunc = typeof(InnerClass).GetMethod("StrFunc", BindingFlags.Instance | BindingFlags.NonPublic); var strFunc = MethodInvokerGenerator.CreateDelegate(mStrFunc); result = strFunc(instance, null); Assert.AreEqual(string.Empty, result); }
public void CallStaticMethods() { var mStaticFunc = typeof(InnerClass).GetMethod("StaticFunc", BindingFlags.Public | BindingFlags.Static); var staticFunc = MethodInvokerGenerator.CreateDelegate(mStaticFunc); var result = staticFunc(null, new object[] { 1, 2 }); Assert.AreEqual(3, result); }
public void CallMethodsThrowsExceptionWithoutArgumentValidation() { var mAct = typeof(InnerClass).GetMethod("Act"); var act = MethodInvokerGenerator.CreateDelegate(mAct, false); var instance = new InnerClass(); Assert.Throws <NullReferenceException>(() => act(null, new object[] { 1 })); Assert.Throws <NullReferenceException>(() => act(instance, null)); Assert.Throws <IndexOutOfRangeException>(() => act(instance, new object[0])); }
public void CallMethodsThrowsExceptionWithArgumentValidation() { var mAct = typeof(InnerClass).GetMethod("Act"); var act = MethodInvokerGenerator.CreateDelegate(mAct); var instance = new InnerClass(); Assert.Throws <ArgumentNullException>(() => act(null, new object[] { 1 })); Assert.Throws <ArgumentNullException>(() => act(instance, null)); Assert.Throws <ArgumentException>(() => act(instance, new object[0])); Assert.Throws <InvalidCastException>(() => act(instance, new object[] { "string" })); }
public void CallOverrideMethods() { var mAct = typeof(InnerClassDerived).GetMethod("Act"); var act = MethodInvokerGenerator.CreateDelegate(mAct); var instance = new InnerClassDerived(); act(instance, new object[] { 123 }); Assert.AreEqual(-123, instance.Value); var mIntFunc = typeof(InnerClass).GetMethod("IntFunc", BindingFlags.Instance | BindingFlags.NonPublic); var intFunc = MethodInvokerGenerator.CreateDelegate(mIntFunc); Assert.AreEqual(2, intFunc(instance, null)); }