public static void Test5() { var test1 = new PropertyGetterTest(3); var methodInfo = typeof(PropertyGetterTest).GetMethod("MyMethod", BindingFlags.Instance | BindingFlags.Public); var methodDelegate1 = (Func <int, int>)Delegate.CreateDelegate(typeof(Func <int, int>), test1, methodInfo); var result1 = methodDelegate1(10); try { var methodDelegate2 = (Func <PropertyGetterTest, int, int>)Delegate.CreateDelegate(typeof(Func <PropertyGetterTest, int, int>), null, methodInfo); var result2 = methodDelegate2(test1, 12); //var methodDelegate3 = (Func<int, int>)Delegate.CreateDelegate(typeof(Func<int, int>), null, methodInfo); //var result3 = methodDelegate3(13); var methodDelegate4 = (Func <PropertyGetterTest, int, int>)Delegate.CreateDelegate(typeof(Func <PropertyGetterTest, int, int>), test1, methodInfo); var result4 = methodDelegate4(test1, 100); } catch (Exception e) { } }
public static void Test2() { var test1 = new PropertyGetterTest(3); var test2 = new PropertyGetterTest(5); var propertyInfo = typeof(PropertyGetterTest).GetProperties(BindingFlags.Instance | BindingFlags.Public) .FirstOrDefault(p => p.Name == "Value"); var propertyGetterDelegate1 = (Func <int>)Delegate.CreateDelegate(typeof(Func <int>), test1, propertyInfo.GetMethod); var propertyGetterDelegate2 = (Func <int>)Delegate.CreateDelegate(typeof(Func <int>), test2, propertyInfo.GetMethod); var result1 = propertyGetterDelegate1(); var result2 = propertyGetterDelegate2(); try { var propertyGetterDelegate3 = (Func <PropertyGetterTest, int>)Delegate.CreateDelegate(typeof(Func <PropertyGetterTest, int>), null, propertyInfo.GetMethod); var result3 = propertyGetterDelegate3(test1); var result4 = propertyGetterDelegate3(test2); } catch (Exception e) { } }
public static void Test3() { var test1 = new PropertyGetterTest(3); var test2 = new PropertyGetterTest(5); var propertyInfo = typeof(PropertyGetterTest).GetProperties(BindingFlags.Instance | BindingFlags.Public) .FirstOrDefault(p => p.Name == "ConstantValue"); var propertyGetterDelegate1 = (Func <int>)Delegate.CreateDelegate(typeof(Func <int>), test1, propertyInfo.GetMethod); var propertyGetterDelegate2 = (Func <int>)Delegate.CreateDelegate(typeof(Func <int>), test2, propertyInfo.GetMethod); var result1 = propertyGetterDelegate1(); var result2 = propertyGetterDelegate2(); }