private void DoPropertyBind(VMBehaviour vm, GameObject obj) { if (this.Component == null) { Debugger.LogError("DataBinder", obj.name + " component null."); return; } if (string.IsNullOrEmpty(this.Property)) { Debugger.LogError("DataBinder", obj.name + " property empty."); return; } if (componentType == null) { componentType = this.Component.GetType(); componentReflection = ReflectionCache.Singleton[componentType]; } PropertyInfo propertyInfo = componentReflection.GetProperty(this.Property); if (propertyInfo == null || propertyInfo.GetSetMethod() == null) { Debugger.LogError("DataBinder", obj.name + " property null or not support."); return; } Type propertyType = propertyInfo.PropertyType; if (!CheckDataTypeValid(propertyType, obj)) { return; } ISetValue propertySetter = SetterWrapper.CreatePropertySetterWrapper(propertyInfo); if (propertySetter == null) { return; } // 数据单向绑定 this.SetValueHandler = delegate(IData source) { object value = this.Source.FastGetValue(); if (this.Converter != null) { value = this.Converter.Convert(value, propertyType, this.Definer.ConverterParameter, vm); } propertySetter.Set(this.Component, value); // ToggleGroup 特殊处理 if (Toggle_Type.IsAssignableFrom(componentType) && this.Property.Equals("isOn")) { Toggle t = this.Component as Toggle; if (t.group != null && t.isOn) { try { t.group.NotifyToggleOn(t); } catch (System.Exception) { } } } }; this.Source.AddValueChangedListener(this.SetValueHandler); this.SetValueHandler.Invoke(this.Source); // 可交互组件的双向绑定 if (Toggle_Type.IsAssignableFrom(componentType) && this.Property.Equals("isOn")) { this.ValueChangedHandler = new UnityAction <bool>(delegate(bool arg) { if (this.Converter != null) { object value = this.Converter.ConvertBack(arg, this.Source.GetBindDataType(), this.Definer.ConverterParameter, vm); this.Source.FastSetValue(value); } else { (this.Source as BaseData <bool>).Set(arg); } }); (this.Component as Toggle).onValueChanged.AddListener((UnityAction <bool>) this.ValueChangedHandler); } if (Input_Type.IsAssignableFrom(componentType) && this.Property.Equals("text")) { this.ValueChangedHandler = new UnityAction <string>(delegate(string arg) { if (this.Converter != null) { object value = this.Converter.ConvertBack(arg, this.Source.GetBindDataType(), this.Definer.ConverterParameter, vm); this.Source.FastSetValue(value); } else { (this.Source as BaseData <string>).Set(arg); } }); (this.Component as InputField).onValueChanged.AddListener((UnityAction <string>) this.ValueChangedHandler); } if (Dropdown_Type.IsAssignableFrom(componentType) && this.Property.Equals("value")) { this.ValueChangedHandler = new UnityAction <int>(delegate(int arg) { if (this.Converter != null) { object value = this.Converter.ConvertBack(arg, this.Source.GetBindDataType(), this.Definer.ConverterParameter, vm); this.Source.FastSetValue(value); } else { (this.Source as BaseData <int>).Set(arg); } }); (this.Component as Dropdown).onValueChanged.AddListener((UnityAction <int>) this.ValueChangedHandler); } if (Slider_Type.IsAssignableFrom(componentType) && this.Property.Equals("value")) { this.ValueChangedHandler = new UnityAction <float>(delegate(float arg) { if (this.Converter != null) { object value = this.Converter.ConvertBack(arg, this.Source.GetBindDataType(), this.Definer.ConverterParameter, vm); this.Source.FastSetValue(value); } else { (this.Source as BaseData <float>).Set(arg); } }); (this.Component as Slider).onValueChanged.AddListener((UnityAction <float>) this.ValueChangedHandler); } }
static string Test_SetProperty(int count) { StringBuilder sb = new StringBuilder(System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion()); sb.AppendLine(); sb.AppendLine("Test_SetProperty"); OrderInfo testObj = new OrderInfo(); PropertyInfo propInfo = typeof(OrderInfo).GetProperty("OrderID"); sb.Append("直接访问花费时间: "); Stopwatch watch1 = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { testObj.OrderID = 123; } watch1.Stop(); sb.AppendLine(watch1.Elapsed.ToString()); SetValueDelegate setter2 = DynamicMethodFactory.CreatePropertySetter(propInfo); sb.Append("EmitSet花费时间: "); Stopwatch watch2 = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { setter2(testObj, 123); } watch2.Stop(); sb.AppendLine(watch2.Elapsed.ToString()); sb.Append("纯反射花费时间: "); Stopwatch watch3 = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { propInfo.SetValue(testObj, 123, null); } watch3.Stop(); sb.AppendLine(watch3.Elapsed.ToString()); sb.Append("泛型委托花费时间: "); SetterWrapper <OrderInfo, int> setter3 = new SetterWrapper <OrderInfo, int>(propInfo); Stopwatch watch4 = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { setter3.SetValue(testObj, 123); } watch4.Stop(); sb.AppendLine(watch4.Elapsed.ToString()); //Console.Write("通用接口花费时间: "); //ISetValue setter4 = GetterSetterFactory.CreatePropertySetterWrapper(propInfo); //Stopwatch watch5 = Stopwatch.StartNew(); //for (int i = 0; i < count; i++) // setter4.Set(testObj, 123); //watch5.Stop(); //Console.WriteLine(watch5.Elapsed.ToString()); //propInfo.FastSetValue(testObj, 123); //Console.Write("FastSet花费时间: "); //Stopwatch watch6 = Stopwatch.StartNew(); //for (int i = 0; i < count; i++) // propInfo.FastSetValue(testObj, 123); //watch6.Stop(); //Console.WriteLine(watch6.Elapsed.ToString()); //propInfo.FastSetValue2(testObj, 123); //Console.Write("FastSet2花费时间: "); //Stopwatch watch6b = Stopwatch.StartNew(); //for (int i = 0; i < count; i++) // propInfo.FastSetValue2(testObj, 123); //watch6b.Stop(); //Console.WriteLine(watch6b.Elapsed.ToString()); //Hashtable table = new Hashtable(); //table[propInfo] = new object(); //Console.Write("Hashtable花费时间: "); //Stopwatch watch7 = Stopwatch.StartNew(); //for (int i = 0; i < count; i++) //{ // object val = table[propInfo]; //} //watch7.Stop(); //Console.WriteLine(watch7.Elapsed.ToString()); sb.AppendLine("-------------------"); sb.Append("纯反射/直接赋值: "); sb.AppendFormat("{0} / {1} = {2}", watch3.Elapsed.ToString(), watch1.Elapsed.ToString(), watch3.Elapsed.TotalMilliseconds / watch1.Elapsed.TotalMilliseconds); sb.AppendLine(); sb.Append("纯反射/EmitSet: "); sb.AppendFormat("{0} / {1} = {2}", watch3.Elapsed.ToString(), watch2.Elapsed.ToString(), watch3.Elapsed.TotalMilliseconds / watch2.Elapsed.TotalMilliseconds); sb.AppendLine(); sb.Append("EmitSet/直接赋值: "); sb.AppendFormat("{0} / {1} = {2}", watch2.Elapsed.ToString(), watch1.Elapsed.ToString(), watch2.Elapsed.TotalMilliseconds / watch1.Elapsed.TotalMilliseconds); //Console.WriteLine("{0} / {1} = {2}", // watch3.Elapsed.ToString(), // watch5.Elapsed.ToString(), // watch3.Elapsed.TotalMilliseconds / watch5.Elapsed.TotalMilliseconds); //Console.WriteLine("{0} / {1} = {2}", // watch3.Elapsed.ToString(), // watch6.Elapsed.ToString(), // watch3.Elapsed.TotalMilliseconds / watch6.Elapsed.TotalMilliseconds); return(sb.ToString()); }