public void GetMethodDelegate_NonPublicStaticMethod() { Type declaringType = typeof(ClassWithMethods); var methodInfo = declaringType.GetMethod("NonPublicStaticMethod", BindingFlags.NonPublic | BindingFlags.Static); var @delegate = (Func <ClassWithMethods, string, string>)DynamicMethodBasedMethodCallerFactory.CreateMethodCallerDelegate( methodInfo, typeof(Func <ClassWithMethods, string, string>)); Assert.That(@delegate(null, "TheValue"), Is.EqualTo("TheValue")); Assert.That(ClassWithMethods.StaticValue, Is.EqualTo("TheValue")); }
public void GetMethodDelegate_PublicInterface_ExplicitInterfaceMethod() { Type declaringType = typeof(IList); var methodInfo = declaringType.GetMethod("Contains", BindingFlags.Public | BindingFlags.Instance); var @delegate = (Func <object, object, bool>)DynamicMethodBasedMethodCallerFactory.CreateMethodCallerDelegate( methodInfo, typeof(Func <object, object, bool>)); var obj = new[] { new object() }; Assert.That(@delegate(obj, obj[0]), Is.True); }
public void GetMethodDelegate_NestedProtectedInterface_ExplicitInterfaceMethod() { Type declaringType = typeof(IProtectedInterfaceWithMethods); var methodInfo = declaringType.GetMethod("ExplicitInterfaceMethod", BindingFlags.Public | BindingFlags.Instance); var @delegate = (Func <object, string, string>)DynamicMethodBasedMethodCallerFactory.CreateMethodCallerDelegate( methodInfo, typeof(Func <object, string, string>)); var obj = new ClassWithMethods(); Assert.That(@delegate(obj, "TheValue"), Is.EqualTo("TheValue")); Assert.That(obj.InstanceValue, Is.EqualTo("TheValue")); }