static void Bind <T>(string prop) { MyObjA myObjA = new MyObjA() { Myi = 1, Myf = 2.0f, Mys = "3" }; Type t = myObjA.GetType(); PropertyInfo propertyInfo = t.GetProperty(prop); MethodInfo getMethodInfo = propertyInfo.GetGetMethod(); Func <T> myFunD = (Func <T>)Delegate.CreateDelegate(typeof(Func <T>), myObjA, getMethodInfo); Console.WriteLine(myFunD()); }
static void Test1() { MyObjA myObjA = new MyObjA() { Myi = 1, Myf = 2.0f, Mys = "3" }; Type t = myObjA.GetType(); PropertyInfo propertyInfo = t.GetProperty("Myi"); MethodInfo getMethodInfo = propertyInfo.GetGetMethod(); Func <int> myiD = (Func <int>)Delegate.CreateDelegate(typeof(Func <int>), myObjA, getMethodInfo); Console.WriteLine(myiD()); }
static void Test3() { MyObjA myObjA = new MyObjA() { Myi = 1, Myf = 2.0f, Mys = "3" }; Type t = myObjA.GetType(); PropertyInfo propertyInfo = t.GetProperty("Mys"); MethodInfo getMethodInfo = propertyInfo.GetGetMethod(); var genType = Type.GetType("System.Func`1[System.String]"); Func <string> myiD = (Func <string>)Delegate.CreateDelegate(genType, myObjA, getMethodInfo); Console.WriteLine(myiD()); }
static void Test2() { MyObjA myObjA = new MyObjA() { Myi = 1, Myf = 2.0f, Mys = "3" }; Type t = myObjA.GetType(); PropertyInfo propertyInfo = t.GetProperty("Myf"); MethodInfo getMethodInfo = propertyInfo.GetGetMethod(); Type funType = typeof(Func <>); Type[] funParamTypes = { typeof(float) }; Type genType = funType.MakeGenericType(funParamTypes); Func <float> myiD = (Func <float>)Delegate.CreateDelegate(genType, myObjA, getMethodInfo); Console.WriteLine(myiD()); }