コード例 #1
0
        /// <summary>
        /// 回放设置对应的属性值
        /// </summary>
        /// <param name="item"></param>
        public void UpdateEntityValue(RecordItem item)
        {
            //获取实体对象实例
            BaseDataModelEntity targetEntity = GetModelEntityByUuid(item.ObjectId);

            if (targetEntity == null)
            {
                targetEntity = RestoreSystem.GetEntity(item.ObjectId);
            }
            if (targetEntity == null)
            {
                return;
            }
            if (item.PropertyPath.Contains("#"))
            {
                UpdateWatchable(targetEntity, item.PropertyPath, item.NewValue);
            }
            //通过Property方式查找属性并设置属性值
            PropertyInfo propertyInfo = null;

            propertyInfo = targetEntity.GetType().GetProperty(item.PropertyPath);
            if (propertyInfo != null)
            {
                if (propertyInfo.PropertyType == typeof(System.Single))
                {
                    System.Single _value = System.Convert.ToSingle(item.NewValue);
                    propertyInfo.SetValue(targetEntity, _value);
                }
                else if (propertyInfo.PropertyType == typeof(System.Int32))
                {
                    System.Int32 _value = System.Convert.ToInt32(item.NewValue);
                    propertyInfo.SetValue(targetEntity, _value);
                }
                else
                {
                    propertyInfo.SetValue(targetEntity, item.NewValue, null);
                }

                return;
            }

            //通过Field方式查找属性并设置属性值
            FieldInfo fieldInfo = targetEntity.GetType().GetField(item.PropertyPath);

            if (fieldInfo != null)
            {
                if (fieldInfo.FieldType == typeof(System.Single))
                {
                    System.Single _value = System.Convert.ToSingle(item.NewValue);
                    fieldInfo.SetValue(targetEntity, _value);
                }
                else if (fieldInfo.FieldType == typeof(System.Int32))
                {
                    System.Int32 _value = System.Convert.ToInt32(item.NewValue);
                    fieldInfo.SetValue(targetEntity, _value);
                }
                else
                {
                    fieldInfo.SetValue(targetEntity, item.NewValue);
                }
            }
        }