Пример #1
0
 /// <summary>
 /// 设置字段值
 /// </summary>
 /// <param name="target">拥有该成员的类实例</param>
 /// <param name="value">字段/属性值</param>
 private void Set(object target, object value)
 {
     if (_set == null)
     {
         _set = FieldAccessor.InitializeSetter(_field);
     }
     value = value ?? TypeUtils.GetNullValue(_field.FieldType);
     _set(target, value);
 }
Пример #2
0
 /// <summary>
 /// 设置属性值
 /// </summary>
 /// <param name="target">拥有该成员的类实例</param>
 /// <param name="value">字段/属性值</param>
 private void Set(object target, object value)
 {
     if (!_property.CanWrite)
     {
         throw new XFrameworkException("{0} is unwritable", base.FullName);
     }
     _set = _set ?? PropertyAccessor.InitializeSetter(_property);
     _set(target, value ?? TypeUtils.GetNullValue(_property.PropertyType));
 }
Пример #3
0
 /// <summary>
 /// 设置字段值
 /// </summary>
 /// <param name="target">拥有该成员的类实例</param>
 /// <param name="value">字段/属性值</param>
 private void Set(object target, object value)
 {
     if (_set == null)
     {
         _set = FieldInvoker.InitializeSetter(_member);
     }
     value = value ?? TypeUtils.GetNullValue(_member.FieldType);
     _set(target, value);
 }
Пример #4
0
 /// <summary>
 /// 设置属性值
 /// </summary>
 /// <param name="target">拥有该成员的类实例</param>
 /// <param name="value">字段/属性值</param>
 private void Set(object target, object value)
 {
     if (!_member.CanWrite)
     {
         throw new XFrameworkException("{0} is unwritable", base.FullName);
     }
     //value = value ?? TypeUtils.GetNullValue(_member.PropertyType);
     //if (value != null)
     //{
     //    if (value.GetType() != this.DataType) value = Convert.ChangeType(value, this.DataType);
     //}
     _set = _set ?? PropertyInvoker.InitializeSetInvoke(this);
     _set(target, value ?? TypeUtils.GetNullValue(_member.PropertyType));
 }
Пример #5
0
 /// <summary>
 /// 动态访问成员
 /// </summary>
 /// <param name="target">拥有该成员的类实例</param>
 /// <param name="parameters">字段/属性值</param>
 public override object Invoke(object target, params object[] parameters)
 {
     if (parameters != null && parameters.Length == 0)
     {
         // get
         object obj = Get(target);
         return(obj);
     }
     else
     {
         // set
         Set(target, parameters != null ? parameters[0] : TypeUtils.GetNullValue(base.DataType));
         return(null);
     }
 }
Пример #6
0
        /// <summary>
        /// 将 <see cref="IDataRecord"/> 上的当前行反序列化为实体
        /// </summary>
        /// <param name="prevModel">前一行数据</param>
        /// <param name="isThisLine">是否同一行数据</param>
        /// <returns></returns>
        internal object Deserialize(object prevModel, out bool isThisLine)
        {
            isThisLine = false;

            #region 基元类型

            if (_isPrimitive == null)
            {
                _isPrimitive = TypeUtils.IsPrimitiveType(_modelType) || _reader.GetName(0) == Constant.AUTO_INCREMENT_NAME;
            }
            if (_isPrimitive.Value)
            {
                if (_reader.IsDBNull(0))
                {
                    return(TypeUtils.GetNullValue(_modelType));                     //default(T);
                }
                var obj = _reader.GetValue(0);
                if (obj.GetType() != _modelType)
                {
                    // fix#Nullable<T> issue
                    if (!_modelType.IsGenericType)
                    {
                        obj = Convert.ChangeType(obj, _modelType);
                    }
                    else
                    {
                        Type type2 = _modelType.GetGenericTypeDefinition();
                        if (type2 != typeof(Nullable <>))
                        {
                            throw new NotSupportedException(string.Format("type {0} not suppored.", type2.FullName));
                        }
                        obj = Convert.ChangeType(obj, Nullable.GetUnderlyingType(_modelType));
                    }
                }

                return(obj);
            }

            #endregion

            #region 动态类型

            if (_isDynamic)
            {
                ExpandoObject obj    = new ExpandoObject();
                var           result = ((IDictionary <string, object>)obj);
                for (int i = 0; i < _reader.FieldCount; i++)
                {
                    var value = _reader.GetValue(i);
                    if (value == DBNull.Value)
                    {
                        value = null;
                    }
                    result.Add(_reader.GetName(i), value);
                }
                return((dynamic)obj);
            }

            #endregion

            #region 实体类型

            object model = null;
            if (_map == null || _map.PickNavDescriptors == null || _map.PickNavDescriptors.Count == 0)
            {
                // 没有字段映射说明或者没有导航属性
                if (_modelDeserializer == null)
                {
                    _modelDeserializer = _deserializerImpl.GetTypeDeserializer(_modelType, _reader, _map != null ? _map.PickColumns : null, 0);
                }
                model = _modelDeserializer(_reader);
            }
            else
            {
                // 第一层
                if (_modelDeserializer == null)
                {
                    _modelDeserializer = _deserializerImpl.GetTypeDeserializer(_modelType, _reader, _map.PickColumns, 0, _map.PickNavDescriptors.MinIndex);
                }
                model = _modelDeserializer(_reader);
                // 若有 1:n 的导航属性,判断当前行数据与上一行数据是否相同
                if (prevModel != null && _map.HasMany)
                {
                    isThisLine = true;
                    foreach (var m in _typeRuntime.KeyMembers)
                    {
                        var value1 = m.Invoke(prevModel);
                        var value2 = m.Invoke(model);
                        isThisLine = isThisLine && value1.Equals(value2);
                        if (!isThisLine)
                        {
                            // Fix issue#换行时清空上一行的导航键缓存
                            _manyNavigationKeys.Clear();
                            break;
                        }
                    }
                }

                // 递归导航属性
                this.Deserialize_Navigation(isThisLine ? prevModel : null, model, string.Empty, isThisLine);
            }

            return(model);

            #endregion
        }