static void BindProp(object source, string sprop, object target, string tprop) { Type st = source.GetType(); Type tt = target.GetType(); PropertyInfo sPropertyInfo = st.GetProperty(sprop); PropertyInfo tPropertyInfo = tt.GetProperty(tprop); MethodInfo getMethodInfo = sPropertyInfo.GetGetMethod(); MethodInfo setMethodInfo = tPropertyInfo.GetSetMethod(); Type spt = sPropertyInfo.PropertyType; Type tpt = tPropertyInfo.PropertyType; var getM = CreateGetter <float>(source, getMethodInfo); var setM = CreateSetter <int>(target, setMethodInfo); MyObjA so = source as MyObjA; MyObjA to = target as MyObjA; DateTime start = DateTime.Now; for (int i = 0; i < N; ++i) { to.Myi = (int)so.Myf; } TimeSpan timeSpan = DateTime.Now - start; Console.WriteLine("BindProp use :" + timeSpan.TotalMilliseconds); }
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 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 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 Test7() { MyObjA myObjA = new MyObjA() { Myi = 1, Myf = 2.0f, Mys = "3" }; MyObjA myObjB = new MyObjA() { Myi = 1, Myf = 2.0f, Mys = "3" }; MyConvert = (Func <float, int>)FloatToInt; Bind2 <float, int>(myObjA, "Myf", myObjB, "Myi", FloatToInt); Bind3 <float, int>(myObjA, "Myf", myObjB, "Myi"); }
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()); }
static void Test6() { MyObjA myObjA = new MyObjA() { Myi = 1, Myf = 2.0f, Mys = "3" }; MyObjA myObjB = new MyObjA() { Myi = 1, Myf = 2.0f, Mys = "3" }; Bind <float, int>(myObjA, "Myf", myObjB, "Myi"); BindInt <float>(myObjA, "Myf", myObjB, "Myi"); Bind(myObjA, "Myf", myObjB, "Myi"); BindByReflect(myObjA, "Myf", myObjB, "Myi"); BindByReflectMethod(myObjA, "Myf", myObjB, "Myi"); BindVar(myObjA, "Myf", myObjB, "Myi"); BindProp(myObjA, "Myf", myObjB, "Myi"); }