Пример #1
0
            /// <summary>
            /// 设置对象值
            /// </summary>
            /// <param name="value"> 待设置的值 </param>
            /// <returns> </returns>
            public bool Set(object value)
            {
                var mapper = new Mapper(value);

                if (mapper.Error != null)
                {
                    _context.AddException(mapper.Error);
                    return(false);
                }
                var row = Instance.NewRow();

                while (mapper.MoveNext())
                {
                    var name = mapper.Key as string;
                    if (name == null)
                    {
                        _context.AddException("标题必须为字符串");
                        return(false);
                    }
                    if (AddCell(row, name, typeof(object), mapper.Value) == false)
                    {
                        return(false);
                    }
                }
                Instance.Rows.Add(row);
                return(true);
            }
Пример #2
0
 /// <summary>
 /// 尝试构造实例,返回是否成功
 /// </summary>
 /// <returns> </returns>
 public bool TryCreateInstance()
 {
     if (_type == null)
     {
         Instance = _dynamic = new ExpandoObject();
         return(true);
     }
     if (_type.IsInterface)
     {
         _context.AddException($"无法创建该接口的实例({_type})");
         return(false);
     }
     if (_type.IsAbstract)
     {
         if (_type.IsSealed)
         {
             _context.AddException("无法创建静态类实例");
         }
         else
         {
             _context.AddException("无法创建抽象类实例");
         }
         return(false);
     }
     try
     {
         Instance = Activator.CreateInstance(_type);
         return(true);
     }
     catch (Exception ex)
     {
         _context.AddException(ex);
         return(false);
     }
 }
Пример #3
0
 public bool Add(object key, object value)
 {
     try
     {
         Instance.Add(key, value);
         return(true);
     }
     catch (Exception ex)
     {
         _context.AddException($"向字典{CType.GetFriendlyName(_type)}中添加元素 {key} 失败,原因:{ex.Message}", ex);
         return(false);
     }
 }
Пример #4
0
 /// <summary>
 /// 设置对象值
 /// </summary>
 /// <param name="value"> 待设置的值 </param>
 /// <returns> </returns>
 public bool Set(object value)
 {
     try
     {
         Instance.Add(value);
         return(true);
     }
     catch (Exception ex)
     {
         _context.AddException($"向集合{CType.GetFriendlyName(_type)}中添加第[{Instance?.Count}]个元素失败,原因:{ex.Message}", ex);
         return(false);
     }
 }
Пример #5
0
 /// <summary>
 /// 尝试构造实例,返回是否成功
 /// </summary>
 /// <returns> </returns>
 public bool TryCreateInstance()
 {
     try
     {
         Instance = (NameValueCollection)Activator.CreateInstance(_type);
         return(true);
     }
     catch (Exception ex)
     {
         _context.AddException(ex);
         return(false);
     }
 }
Пример #6
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        /// <returns> </returns>
        protected override DataTable ChangeTypeImpl(ConvertContext context, object input, Type outputType,
                                                    out bool success)
        {
            success = true;
            var view = input as DataView;

            if (view != null)
            {
                return(view.ToTable());
            }
            var reader = input as IDataReader;

            if (reader != null)
            {
                if (reader.IsClosed)
                {
                    context.AddException("DataReader已经关闭");
                    success = false;
                    return(null);
                }
                var table1 = new DataTable();
                table1.Load(reader);
                return(table1);
            }

            var ee = (input as IEnumerable)?.GetEnumerator()
                     ?? input as IEnumerator
                     ?? (input as DataTable)?.Rows.GetEnumerator()
                     ?? (input as DataRow)?.ItemArray.GetEnumerator()
                     ?? (input as DataRowView)?.Row.ItemArray.GetEnumerator()
                     ?? (input as IListSource)?.GetList()?.GetEnumerator();

            if (ee == null)
            {
                context.AddException("仅支持DataView,DataRow,DataRowView,或实现IEnumerator,IEnumerable,IListSource,IDataReader接口的对象对DataTable的转换");
                success = false;
                return(null);
            }
            var builder = new DataTableBuilder(context);

            builder.TryCreateInstance();
            while (ee.MoveNext())
            {
                if (builder.Set(ee.Current) == false)
                {
                    success = false;
                    return(null);
                }
            }
            return(builder.Instance);
        }
Пример #7
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override IList ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            input = input?.Trim();
            if ((input == null) || (input.Length <= 1))
            {
                success = false;
                return(null);
            }
            if ((input[0] == '[') && (input[input.Length - 1] == ']'))
            {
                try
                {
                    var result = ComponentServices.ToJsonObject(outputType, input);
                    success = true;
                    return((IList)result);
                }
                catch (Exception ex)
                {
                    context.AddException(ex);
                    success = false;
                    return(null);
                }
            }
            var arr = input.Split(_Separator, StringSplitOptions.None);

            return(ChangeType(context, arr, outputType, out success));
        }
Пример #8
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override Uri ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            if (input == null)
            {
                success = true;
                return(null);
            }
            Uri result;

            input = input.TrimStart();
            if ((input.Length > 10) && (input[6] != '/'))
            {
                if (Uri.TryCreate("http://" + input, UriKind.Absolute, out result))
                {
                    success = true;
                    return(result);
                }
            }

            if (Uri.TryCreate(input, UriKind.Absolute, out result))
            {
                success = true;
                return(result);
            }

            context.AddException(input + "不是一个有效的url");
            success = false;
            return(null);
        }
Пример #9
0
 /// <summary>
 /// 返回指定类型的对象,其值等效于指定字符串对象。
 /// </summary>
 /// <param name="context"> </param>
 /// <param name="input"> 需要转换类型的字符串对象 </param>
 /// <param name="outputType"> 换转后的类型 </param>
 /// <param name="success"> 是否成功 </param>
 protected override IDictionary ChangeType(ConvertContext context, string input, Type outputType,
                                           out bool success)
 {
     if (string.IsNullOrWhiteSpace(input))
     {
         success = false;
         return(null);
     }
     input = input.Trim();
     if ((input[0] == '{') && (input[input.Length - 1] == '}'))
     {
         try
         {
             var result = ComponentServices.ToJsonObject(outputType, input);
             success = true;
             return((IDictionary)result);
         }
         catch (Exception ex)
         {
             context.AddException(ex);
         }
     }
     success = false;
     return(null);
 }
Пример #10
0
 /// <summary>
 /// 返回指定类型的对象,其值等效于指定对象。
 /// </summary>
 /// <param name="context"> </param>
 /// <param name="input"> 需要转换类型的对象 </param>
 /// <param name="outputType"> 换转后的类型 </param>
 /// <param name="success"> 是否成功 </param>
 /// <returns> </returns>
 protected override ValueType ChangeTypeImpl(ConvertContext context, object input, Type outputType,
                                             out bool success)
 {
     context.AddException("无法为值类型(struct)提供转换");
     success = false;
     return(null);
 }
Пример #11
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override object ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            try
            {
                if (outputType == typeof(object))
                {
                    success = true;
                    return(input);
                }
                var type = Type.GetType(input, false, true);
                if (type == null)
                {
                    success = false;
                    return(null);
                }
                if (outputType.IsAssignableFrom(type) == false)
                {
                    success = false;
                    return(null);
                }

                success = true;
                return(Activator.CreateInstance(type, true));
            }
            catch (Exception ex)
            {
                context.AddException(ex);
            }
            success = false;
            return(null);
        }
Пример #12
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override ICollection <T> ChangeType(ConvertContext context, string input, Type outputType,
                                                      out bool success)
        {
            input = input.Trim();
            if (input.Length == 0)
            {
                var builder = new ListBuilder(context, outputType);
                // ReSharper disable once AssignmentInConditionalExpression
                return((success = builder.TryCreateInstance()) ? builder.Instance : null);
            }
            if ((input[0] == '[') && (input[input.Length - 1] == ']'))
            {
                try
                {
                    var result = ComponentServices.ToJsonObject(outputType, input);
                    success = true;
                    return((ICollection <T>)result);
                }
                catch (Exception ex)
                {
                    context.AddException(ex);
                    success = false;
                    return(null);
                }
            }
            var arr = input.Split(_Separator, StringSplitOptions.None);

            return(ChangeType(context, arr, outputType, out success));
        }
Пример #13
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override IDictionary <K, V> ChangeType(ConvertContext context, string input, Type outputType,
                                                         out bool success)
        {
            input = input?.Trim() ?? "";
            if (input.Length == 0)
            {
                var keyConvertor   = context.Get <K>();
                var valueConvertor = context.Get <V>();

                var builder = new DictionaryBuilder(context, outputType, keyConvertor, valueConvertor);
                // ReSharper disable once AssignmentInConditionalExpression
                return((success = builder.TryCreateInstance()) ? builder.Instance : null);
            }
            if (input?.Length > 2)
            {
                if ((input[0] == '{') && (input[input.Length - 1] == '}'))
                {
                    try
                    {
                        var result = ComponentServices.ToJsonObject(outputType, input);
                        success = true;
                        return((IDictionary <K, V>)result);
                    }
                    catch (Exception ex)
                    {
                        context.AddException(ex);
                    }
                }
            }
            success = false;
            return(null);
        }
Пример #14
0
 /// <summary>
 /// 尝试构造实例,返回是否成功
 /// </summary>
 /// <returns></returns>
 public bool TryCreateInstance()
 {
     if (_type.IsInterface)
     {
         Instance = new Dictionary <K, V>();
         return(true);
     }
     try
     {
         Instance = (IDictionary <K, V>)Activator.CreateInstance(_type);
         return(true);
     }
     catch (Exception ex)
     {
         _context.AddException(ex);
         return(false);
     }
 }
Пример #15
0
 /// <summary>
 /// 返回指定类型的对象,其值等效于指定对象。
 /// </summary>
 /// <param name="context"> </param>
 /// <param name="input"> 需要转换类型的对象 </param>
 /// <param name="outputType"> 换转后的类型 </param>
 /// <param name="success"> 是否成功 </param>
 protected sealed override T ChangeType(ConvertContext context, object input, Type outputType, out bool success)
 {
     if (outputType.IsGenericTypeDefinition)
     {
         context.AddException("无法转为泛型定义类");
         success = false;
         return(default(T));
     }
     return(ChangeTypeImpl(context, input, outputType, out success));
 }
Пример #16
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override T ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                success = false;
                return(default(T));
            }
            T result;

            try
            {
                result = (T)Enum.Parse(outputType, input, true);
            }
            catch (Exception ex)
            {
                context.AddException(ex);
                success = false;
                return(default(T));
            }

            if (result.Equals(default(T)))
            {
                success = true;
                return(default(T));
            }
            if (Enum.IsDefined(outputType, result))
            {
                success = true;
                return(result);
            }
            if (Attribute.IsDefined(outputType, typeof(FlagsAttribute)))
            {
                if (result.ToString().Contains(","))
                {
                    success = true;
                    return(result);
                }
            }
            context.AddException($"{result} 不是有效的枚举值");
            success = false;
            return(default(T));
        }
Пример #17
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected T BaseChangeType(ConvertContext context, object input, Type outputType, out bool success)
        {
            if ((input == null) || input is DBNull)
            {
                return(ChangeType(context, input, outputType, out success));
            }
            var str = input as string;

            if (str != null)
            {
                return(BaseChangeType(context, str, outputType, out success));
            }
            success = false;
            try
            {
                //类型相同直接转换
                if (outputType.IsInstanceOfType(input))
                {
                    success = true;
                    return((T)input);
                }

                //子类转换逻辑
                var result = ChangeType(context, input, outputType, out success);
                if (success)
                {
                    return(result);
                }
                if (ShouldConvertString)
                {
                    var snapshot = context.Snapshot();
                    //尝试转string后转换
                    str = input.ToString();
                    context.AddException($"尝试将{input.GetType()}转为字符串 = \"{str}\"");
                    result = ((IConvertor <T>) this).ChangeType(context, str, outputType, out success);
                    if (success)
                    {
                        snapshot.Recovery();
                    }
                    return(result);
                }
                else
                {
                    return(default(T));
                }
            }
            finally
            {
                if (success == false)
                {
                    context.AddCastFailException(input, outputType);
                }
            }
        }
Пример #18
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override T[] ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            input = input?.Trim() ?? "";
            if (input.Length == 0)
            {
                success = true;
                return((T[])Array.CreateInstance(outputType.GetElementType(), 0));
            }
            if ((input[0] == '[') && (input[input.Length - 1] == ']')) //判断如果是json字符串,使用json方式转换
            {
                try
                {
                    var result = ComponentServices.ToJsonObject(outputType, input);
                    success = true;
                    return((T[])result);
                }
                catch (Exception ex)
                {
                    context.AddException(ex);
                    success = false;
                    return(null);
                }
            }

            var convertor = context.Get <T>();
            var items     = input.Split(_Separator, StringSplitOptions.None);
            var array     = Array.CreateInstance(convertor.OutputType, items.Length);

            for (var i = 0; i < items.Length; i++)
            {
                var value = convertor.ChangeType(context, items[i], convertor.OutputType, out success);
                if (success == false)
                {
                    context.AddException($"{value?.ToString() ?? "<null>"} 写入数组失败!");
                    return(null);
                }
                array.SetValue(value, i);
            }
            success = true;
            return((T[])array);
        }
Пример #19
0
            /// <summary>
            /// 设置对象值
            /// </summary>
            /// <param name="obj"> 待设置的值 </param>
            /// <returns> </returns>
            public bool Set(object obj)
            {
                bool b;
                var  v = _convertor.ChangeType(_context, obj, _convertor.OutputType, out b);

                if (b == false)
                {
                    _context.AddException($"向集合{CType.GetFriendlyName(_type)}中添加第[{Instance?.Count}]个元素失败");
                    return(false);
                }
                try
                {
                    Instance.Add(v);
                    return(true);
                }
                catch (Exception ex)
                {
                    _context.AddException($"向集合{CType.GetFriendlyName(_type)}中添加第[{Instance?.Count}]个元素失败,原因:{ex.Message}", ex);
                    return(false);
                }
            }
Пример #20
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override object ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            if (input?.Length > 2)
            {
                switch (input[0])
                {
                case '"':
                    if (input[input.Length - 1] != '"')
                    {
                        return(base.ChangeType(context, input, outputType, out success));
                    }
                    break;

                case '\'':
                    if (input[input.Length - 1] != '\'')
                    {
                        return(base.ChangeType(context, input, outputType, out success));
                    }
                    break;

                case '{':
                    if (input[input.Length - 1] != '}')
                    {
                        return(base.ChangeType(context, input, outputType, out success));
                    }
                    break;

                case '[':
                    if (input[input.Length - 1] != ']')
                    {
                        return(base.ChangeType(context, input, outputType, out success));
                    }
                    break;

                default:
                    return(base.ChangeType(context, input, outputType, out success));
                }
                try
                {
                    success = true;
                    return(ComponentServices.ToJsonObject(outputType, input));
                }
                catch (Exception ex)
                {
                    context.AddException(ex);
                }
            }
            return(base.ChangeType(context, input, outputType, out success));
        }
Пример #21
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        /// <returns> </returns>
        protected override T[] ChangeTypeImpl(ConvertContext context, object input, Type outputType, out bool success)
        {
            if ((input == null) || input is DBNull)
            {
                success = true;
                return(null);
            }

            var convertor   = context.Get <T>();
            var array       = new ArrayList();
            var elementType = convertor.OutputType;

            //获取对象的枚举器
            var ee = (input as IEnumerable)?.GetEnumerator()
                     ?? input as IEnumerator;

            if (ee == null)
            {
                var value = convertor.ChangeType(context, input, elementType, out success);
                if (success == false)
                {
                    success = false;
                    return(null);
                }
                array.Add(value);
                success = true;
                return((T[])array.ToArray(elementType)); //输出数组
            }

            while (ee.MoveNext()) //循环转换枚举器中的对象,并构造数组
            {
                var value = convertor.ChangeType(context, ee.Current, elementType, out success);
                if (success == false)
                {
                    context.AddException($"{value?.ToString() ?? "<null>"} 写入数组失败!");
                    return(null);
                }
                array.Add(value);
            }
            success = true;
            return((T[])array.ToArray(elementType)); //输出数组
        }
Пример #22
0
 /// <summary>
 /// 返回指定类型的对象,其值等效于指定字符串对象。
 /// </summary>
 /// <param name="context"> </param>
 /// <param name="input"> 需要转换类型的字符串对象 </param>
 /// <param name="outputType"> 换转后的类型 </param>
 /// <param name="success"> 是否成功 </param>
 protected override NameValueCollection ChangeType(ConvertContext context, string input, Type outputType,
                                                   out bool success)
 {
     input = input?.Trim();
     if ((input?.Length > 1) && (input[0] == '{') && (input[input.Length - 1] == '}'))
     {
         try
         {
             var result = ComponentServices.ToJsonObject(outputType, input);
             success = true;
             return((NameValueCollection)result);
         }
         catch (Exception ex)
         {
             context.AddException(ex);
         }
     }
     success = false;
     return(null);
 }
Пример #23
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        /// <returns> </returns>
        protected override IDictionary <K, V> ChangeTypeImpl(ConvertContext context, object input, Type outputType,
                                                             out bool success)
        {
            if (input.IsNull())
            {
                success = true;
                return(null);
            }

            var keyConvertor   = context.Get <K>();
            var valueConvertor = context.Get <V>();


            var builder = new DictionaryBuilder(context, outputType, keyConvertor, valueConvertor);

            if (builder.TryCreateInstance() == false)
            {
                success = false;
                return(null);
            }
            var mapper = new Mapper(input);

            if (mapper.Error != null)
            {
                context.AddException(mapper.Error);
                success = false;
                return(null);
            }

            while (mapper.MoveNext())
            {
                if (builder.Add(mapper.Key, mapper.Value) == false)
                {
                    success = false;
                    return(null);
                }
            }

            success = true;
            return(builder.Instance);
        }
Пример #24
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override Type ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            if (input == null)
            {
                success = true;
                return(null);
            }
            var result = Type.GetType(_Keywords[input] ?? input, false, true) ??
                         Type.GetType("System." + input, false, true);

            if (result == null)
            {
                context.AddException(new TypeLoadException(input + " 并不是一个类型"));
                success = false;
            }
            else
            {
                success = true;
            }
            return(result);
        }
Пример #25
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        /// <returns> </returns>
        protected override IDictionary ChangeTypeImpl(ConvertContext context, object input, Type outputType,
                                                      out bool success)
        {
            if ((input == null) || input is DBNull)
            {
                success = true;
                return(null);
            }

            var builder = new DictionaryBuilder(outputType, context);

            if (builder.TryCreateInstance() == false)
            {
                success = false;
                return(null);
            }

            var mapper = new Mapper(input);

            if (mapper.Error != null)
            {
                context.AddException(mapper.Error);
                success = false;
                return(null);
            }

            while (mapper.MoveNext())
            {
                if (builder.Add(mapper.Key, mapper.Value) == false)
                {
                    success = false;
                    return(null);
                }
            }

            success = true;
            return(builder.Instance);
        }
Пример #26
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        /// <returns> </returns>
        protected override NameValueCollection ChangeTypeImpl(ConvertContext context, object input, Type outputType,
                                                              out bool success)
        {
            if (input.IsNull())
            {
                success = true;
                return(null);
            }
            var builder = new NVCollectiontBuilder(context, outputType);

            if (builder.TryCreateInstance() == false)
            {
                success = false;
                return(null);
            }

            var mapper = new Mapper(input);

            if (mapper.Error != null)
            {
                context.AddException(mapper.Error);
                success = false;
                return(null);
            }

            while (mapper.MoveNext())
            {
                if (builder.Add(mapper.Key, mapper.Value) == false)
                {
                    success = false;
                    return(null);
                }
            }

            success = true;
            return(builder.Instance);
        }
Пример #27
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override string ChangeType(ConvertContext context, object input, Type outputType, out bool success)
        {
            success = true;
            if (input is DataRow || input is DataRowView)
            {
                return(ComponentServices.ToJsonString(input));
            }

            var reader = input as IDataReader;

            if (reader != null)
            {
                if (reader.IsClosed)
                {
                    success = false;
                    context.AddException("DataReader已经关闭");
                    return(null);
                }
                switch (reader.FieldCount)
                {
                case 0:
                    return(null);

                case 1:
                    return(BaseChangeType(context, reader.GetValue(0), outputType, out success));

                default:
                    return(ComponentServices.ToJsonString(input));
                }
            }

            if (input.IsNull())
            {
                return(null);
            }

            if (input is bool)
            {
                return((bool)input ? "true" : "false");
            }

            var convertible = input as IConvertible;

            if (convertible != null)
            {
                return(convertible.ToString(null));
            }

            var formattable = input as IFormattable;

            if (formattable != null)
            {
                return(formattable.ToString(null, null));
            }

            var type = input as Type;

            if (type != null)
            {
                return(CType.GetFriendlyName(type));
            }

            var bs = input as byte[];

            if (bs != null)
            {
                return(Encoding.UTF8.GetString(bs));
            }

            var ps = input.GetType().GetProperties();

            if (ps.Length > 0)
            {
                return(ComponentServices.ToJsonString(input));
            }

            return(input.ToString());
        }
Пример #28
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        /// <returns> </returns>
        protected override ICollection <T> ChangeTypeImpl(ConvertContext context, object input, Type outputType,
                                                          out bool success)
        {
            success = true;
            if (input.IsNull())
            {
                return(null);
            }
            var builder = new ListBuilder(context, outputType);

            if (builder.TryCreateInstance() == false)
            {
                success = false;
                return(null);
            }
            var reader = input as IDataReader;

            if (reader != null)
            {
                if (reader.IsClosed)
                {
                    context.AddException("DataReader已经关闭");
                    success = false;
                    return(null);
                }
                while (reader.Read())
                {
                    if (builder.Set(reader))
                    {
                        continue;
                    }
                    success = false;
                    return(null);
                }
                return(builder.Instance);
            }

            var ee = (input as IEnumerable)?.GetEnumerator()
                     ?? input as IEnumerator
                     ?? (input as DataTable)?.Rows.GetEnumerator()
                     ?? (input as DataRow)?.ItemArray.GetEnumerator()
                     ?? (input as DataRowView)?.Row.ItemArray.GetEnumerator()
                     ?? (input as IListSource)?.GetList()?.GetEnumerator();

            if (ee == null)
            {
                if (builder.Set(input))
                {
                    return(builder.Instance);
                }
                success = false;
                return(null);
            }

            while (ee.MoveNext())
            {
                if (builder.Set(ee.Current))
                {
                    continue;
                }
                success = false;
                return(null);
            }
            return(builder.Instance);
        }
Пример #29
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected sealed override T ChangeType(ConvertContext context, object input, Type outputType, out bool success)
        {
            if (input.IsNull())
            {
                return(ChangeTypeImpl(context, null, outputType, out success));
            }
            var row = input as DataRow;

            if (row != null)
            {
                switch (row.Table.Columns.Count)
                {
                case 0:
                    return(ChangeTypeImpl(context, null, outputType, out success));

                case 1:
                    return(BaseChangeType(context, row[0], outputType, out success));

                default:
                    success = false;
                    context.AddException("只有当 DataRow 有且只有一列时才能尝试转换");
                    return(default(T));
                }
            }

            var rv = input as DataRowView;

            if (rv != null)
            {
                switch (rv.DataView.Table.Columns.Count)
                {
                case 0:
                    return(ChangeTypeImpl(context, null, outputType, out success));

                case 1:
                    return(BaseChangeType(context, rv[0], outputType, out success));

                default:
                    success = false;
                    context.AddException("只有当 DataRowView 有且只有一列时才能尝试转换");
                    return(default(T));
                }
            }

            var reader = input as IDataReader;

            if (reader != null)
            {
                switch (reader.FieldCount)
                {
                case 0:
                    return(ChangeTypeImpl(context, null, outputType, out success));

                case 1:
                    return(BaseChangeType(context, reader.GetValue(0), outputType, out success));

                default:
                    success = false;
                    context.AddException("只有当 IDataReader 有且只有一列时才能尝试转换");
                    return(default(T));
                }
            }

            var ee = (input as IEnumerable)?.GetEnumerator() ?? input as IEnumerator;

            if (ee?.MoveNext() == true)
            {
                var value = ee.Current;
                if (ee.MoveNext())
                {
                    success = false;
                    context.AddException("只有当 集合 有且只有一行时才能尝试转换");
                    return(default(T));
                }
                return(BaseChangeType(context, value, outputType, out success));
            }

            return(ChangeTypeImpl(context, input, outputType, out success));
        }
Пример #30
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override T ChangeTypeImpl(ConvertContext context, object input, Type outputType, out bool success)
        {
            var conv = input as IConvertible;

            if (conv != null)
            {
                T result;
                switch (conv.GetTypeCode())
                {
                case TypeCode.Empty:
                case TypeCode.DBNull:
                case TypeCode.DateTime:
                case TypeCode.Boolean:
                    success = false;
                    return(default(T));

                case TypeCode.Decimal:
                case TypeCode.Char:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.SByte:
                case TypeCode.Double:
                case TypeCode.Single:
                    result = (T)Enum.ToObject(outputType, conv.ToInt64(null));
                    break;

                case TypeCode.Byte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    result = (T)Enum.ToObject(outputType, conv.ToUInt64(null));
                    break;

                case TypeCode.String:
                    return(ChangeType(context, conv.ToString(null), outputType, out success));

                case TypeCode.Object:
                default:
                    success = false;
                    return(default(T));
                }
                if (result.Equals(default(T)))
                {
                    success = true;
                    return(result);
                }
                if (Enum.IsDefined(outputType, result))
                {
                    success = true;
                    return(result);
                }
                if (Attribute.IsDefined(outputType, typeof(FlagsAttribute)))
                {
                    if (result.ToString().Contains(","))
                    {
                        success = true;
                        return(result);
                    }
                }
                context.AddException($"{result} 不是有效的枚举值");
            }
            success = false;
            return(default(T));
        }