public void TestRefOutMethods() { IDynamicMethod refMethod = DynamicMethod.Create(typeof(MethodTarget).GetMethod("MethodWithRefParameter")); MethodTarget target = new MethodTarget(); object[] args = new object[] { "aleks", 5 }; refMethod.Invoke(target, args); Assert.AreEqual("ALEKS", args[0]); Assert.AreEqual(25, args[1]); IDynamicMethod outMethod = DynamicMethod.Create(typeof(MethodTarget).GetMethod("MethodWithOutParameter")); args = new object[] { "aleks", null }; outMethod.Invoke(target, args); Assert.AreEqual("ALEKS", args[1]); IDynamicMethod refOutMethod = DynamicMethod.Create(typeof(RefOutTestObject).GetMethod("DoIt")); RefOutTestObject refOutTarget = new RefOutTestObject(); args = new object[] { 0, 1, null }; refOutMethod.Invoke(refOutTarget, args); Assert.AreEqual(2, args[1]); Assert.AreEqual("done", args[2]); refOutMethod.Invoke(refOutTarget, args); Assert.AreEqual(3, args[1]); Assert.AreEqual("done", args[2]); int count = 0; string done; target.DoItCaller(0, ref count, out done); Assert.AreEqual(1, count); Assert.AreEqual("done", done); }
public void TestInstanceMethods() { IDynamicMethod getAge = DynamicMethod.Create(typeof(Inventor).GetMethod("GetAge")); Assert.AreEqual(tesla.GetAge(DateTime.Today), getAge.Invoke(tesla, new object[] { DateTime.Today })); MethodTarget target = new MethodTarget(); IDynamicMethod test = DynamicMethod.Create(typeof(MethodTarget).GetMethod("MethodReturningString")); Assert.AreEqual(tesla.Name, test.Invoke(target, new object[] { 5, DateTime.Today, new String[] { "xyz", "abc" }, tesla })); ArrayList list = new ArrayList(new string[] { "one", "two", "three" }); IDynamicMethod removeAt = DynamicMethod.Create(typeof(ArrayList).GetMethod("RemoveAt")); removeAt.Invoke(list, new object[] { 1 }); Assert.AreEqual(2, list.Count); Assert.AreEqual("three", list[1]); }