예제 #1
0
        /// <summary>
        /// 給指定对象的指定字段赋值
        /// </summary>
        /// <param name="field">字段信息</param>
        /// <param name="value">值</param>
        /// <param name="target">指定对象</param>
        /// <returns></returns>
        public static ParameterErrorEnum SetFieldValue(FieldInfo field, object value, object target)
        {
            if (field == null)
            {
                return(ParameterErrorEnum.NoFieldFoundInObject);
            }
            if (IsNull(value))
            {
                return(ParameterErrorEnum.ParameterNotFound);
            }
            ParameterErrorEnum result = ParameterErrorEnum.UnknownParameterType;
            var isArray = field.FieldType.IsArray;

            if (isArray)
            {
                var elementType = field.FieldType.GetElementType();
                var array       = (Array)value;
                if (array.Length == 0)
                {
                    return(ParameterErrorEnum.ParameterNotFound);
                }
                if (ConvertMapping.ContainsToObjectType(elementType))
                {
                    Array ret = Array.CreateInstance(elementType, array.Length);
                    for (int i = 0; i < array.Length; i++)
                    {
                        var    obj            = array.GetValue(i);
                        object convertedValue = null;
                        try
                        {
                            if (obj.GetType() == elementType)
                            {
                                convertedValue = obj;
                            }
                            else
                            {
                                convertedValue = ConvertMapping.ConvertToObject(elementType, obj.ToString().Trim());
                            }
                            ret.SetValue(convertedValue, i);
                        }
                        catch
                        {
                            result = ParameterErrorEnum.ExceptionAppeared;
                            break;
                        }
                    }
                    try
                    {
                        field.SetValue(target, ret);
                        result = ParameterErrorEnum.NoError;
                    }
                    catch
                    {
                        result = ParameterErrorEnum.SetParameterError;
                    }
                }
                return(result);
            }
            else
            {
                if (ConvertMapping.ContainsToObjectType(field.FieldType))
                {
                    object convertedValue = null;
                    try
                    {
                        if (value.GetType() == field.FieldType)
                        {
                            convertedValue = value;
                        }
                        else
                        {
                            convertedValue = ConvertMapping.ConvertToObject(field.FieldType, value.ToString().Trim());
                        }
                    }
                    catch
                    {
                        result = ParameterErrorEnum.ExceptionAppeared;
                    }
                    try
                    {
                        field.SetValue(target, convertedValue);
                        result = ParameterErrorEnum.NoError;
                    }
                    catch
                    {
                        result = ParameterErrorEnum.SetParameterError;
                    }
                }
                else
                {
                    result = ParameterErrorEnum.UnknownParameterType;
                }

                return(result);
            }
        }
예제 #2
0
 /// <summary>
 /// 为给定类型对象提供字符串到对象的转换方法
 /// </summary>
 /// <param name="type"></param>
 /// <param name="method">ConvertMethodHandler委托,这是一个输入string值,返回object对象的方法</param>
 public static void SetConvertMapping(Type type, Delegate method)
 {
     ConvertMapping.SetMappingToObject(type, method, true);
 }
예제 #3
0
        static ConvertMapping()
        {
            #region ToObject

            SetMappingToObject(SystemTypes.Boolean, new ConvertMethodHandlerT <bool>(Oct.ToBoolean));
            SetMappingToObject(SystemTypes.BooleanArray, new ConvertMethodHandlerT <bool[]>(Oct.ToBooleanArray));
            SetMappingToObject(SystemTypes.Byte, new ConvertMethodHandlerT <byte>(Oct.ToByte));
            SetMappingToObject(SystemTypes.ByteArray, new ConvertMethodHandlerT <byte[]>(Oct.ToByteArray));
            SetMappingToObject(SystemTypes.Char, new ConvertMethodHandlerT <char>(Oct.ToChar));
            SetMappingToObject(SystemTypes.CharArray, new ConvertMethodHandlerT <char[]>(Oct.ToCharArray));
#if !SILVERLIGHT
            SetMappingToObject(SystemTypes.Color, new ConvertMethodHandlerT <Color>(Oct.ToColor));
            SetMappingToObject(SystemTypes.ColorArray, new ConvertMethodHandlerT <Color[]>(Oct.ToColorArray));
#endif
            SetMappingToObject(SystemTypes.DateTime, new ConvertMethodHandlerT <DateTime>(Oct.ToDateTime));
            SetMappingToObject(SystemTypes.DateTimeArray, new ConvertMethodHandlerT <DateTime[]>(Oct.ToDateTimeArray));
            SetMappingToObject(SystemTypes.Decimal, new ConvertMethodHandlerT <Decimal>(Oct.ToDecimal));
            SetMappingToObject(SystemTypes.DecimalArray, new ConvertMethodHandlerT <Decimal[]>(Oct.ToDecimalArray));
            SetMappingToObject(SystemTypes.Double, new ConvertMethodHandlerT <Double>(Oct.ToDouble));
            SetMappingToObject(SystemTypes.DoubleArray, new ConvertMethodHandlerT <Double[]>(Oct.ToDoubleArray));
            SetMappingToObject(SystemTypes.Int16, new ConvertMethodHandlerT <Int16>(Oct.ToInt16));
            SetMappingToObject(SystemTypes.Int16Array, new ConvertMethodHandlerT <Int16[]>(Oct.ToInt16Array));
            SetMappingToObject(SystemTypes.Int32, new ConvertMethodHandlerT <Int32>(Oct.ToInt32));
            SetMappingToObject(SystemTypes.Int32Array, new ConvertMethodHandlerT <Int32[]>(Oct.ToInt32Array));
            SetMappingToObject(SystemTypes.Int64, new ConvertMethodHandlerT <Int64>(Oct.ToInt64));
            SetMappingToObject(SystemTypes.Int64Array, new ConvertMethodHandlerT <Int64[]>(Oct.ToInt64Array));
#if !SILVERLIGHT
            SetMappingToObject(SystemTypes.Point, new ConvertMethodHandlerT <Point>(Oct.ToPoint));
            SetMappingToObject(SystemTypes.PointArray, new ConvertMethodHandlerT <Point[]>(Oct.ToPointArray));
            SetMappingToObject(SystemTypes.PointF, new ConvertMethodHandlerT <PointF>(Oct.ToPointF));
            SetMappingToObject(SystemTypes.PointFArray, new ConvertMethodHandlerT <PointF[]>(Oct.ToPointFArray));
            SetMappingToObject(SystemTypes.Rectangle, new ConvertMethodHandlerT <Rectangle>(Oct.ToRectangle));
            SetMappingToObject(SystemTypes.RectangleArray, new ConvertMethodHandlerT <Rectangle[]>(Oct.ToRectangleArray));
            SetMappingToObject(SystemTypes.RectangleF, new ConvertMethodHandlerT <RectangleF>(Oct.ToRectangleF));
            SetMappingToObject(SystemTypes.RectangleFArray, new ConvertMethodHandlerT <RectangleF[]>(Oct.ToRectangleFArray));
            SetMappingToObject(SystemTypes.Size, new ConvertMethodHandlerT <Size>(Oct.ToSize));
            SetMappingToObject(SystemTypes.SizeArray, new ConvertMethodHandlerT <Size[]>(Oct.ToSizeArray));
            SetMappingToObject(SystemTypes.SizeF, new ConvertMethodHandlerT <SizeF>(Oct.ToSizeF));
            SetMappingToObject(SystemTypes.SizeFArray, new ConvertMethodHandlerT <SizeF[]>(Oct.ToSizeFArray));
#endif
            SetMappingToObject(SystemTypes.SByte, new ConvertMethodHandlerT <SByte>(Oct.ToSByte));
            SetMappingToObject(SystemTypes.SByteArray, new ConvertMethodHandlerT <SByte[]>(Oct.ToSByteArray));
            SetMappingToObject(SystemTypes.Single, new ConvertMethodHandlerT <Single>(Oct.ToSingle));
            SetMappingToObject(SystemTypes.SingleArray, new ConvertMethodHandlerT <Single[]>(Oct.ToSingleArray));
            SetMappingToObject(SystemTypes.String, new ConvertMethodHandlerT <String>(Oct.ToString));
            SetMappingToObject(SystemTypes.StringArray, new ConvertMethodHandlerT <String[]>(Oct.ToStringArray));
            SetMappingToObject(SystemTypes.TimeSpan, new ConvertMethodHandlerT <TimeSpan>(Oct.ToTimeSpan));
            SetMappingToObject(SystemTypes.TimeSpanArray, new ConvertMethodHandlerT <TimeSpan[]>(Oct.ToTimeSpaneArray));
            SetMappingToObject(SystemTypes.UInt16, new ConvertMethodHandlerT <UInt16>(Oct.ToUInt16));
            SetMappingToObject(SystemTypes.UInt16Array, new ConvertMethodHandlerT <UInt16[]>(Oct.ToUInt16Array));
            SetMappingToObject(SystemTypes.UInt32, new ConvertMethodHandlerT <UInt32>(Oct.ToUInt32));
            SetMappingToObject(SystemTypes.UInt32Array, new ConvertMethodHandlerT <UInt32[]>(Oct.ToUInt32Array));
            SetMappingToObject(SystemTypes.UInt64, new ConvertMethodHandlerT <UInt64>(Oct.ToUInt64));
            SetMappingToObject(SystemTypes.UInt64Array, new ConvertMethodHandlerT <UInt64[]>(Oct.ToUInt64Array));

#if !SILVERLIGHT
            SetMappingToObject(typeof(IPEndPoint), new ConvertMethodHandlerT <IPEndPoint>(Oct.ToEndPoint));
            SetMappingToObject(typeof(IPEndPoint[]), new ConvertMethodHandlerT <IPEndPoint[]>(Oct.ToEndPointArray));
#endif

            ConvertMapping.SetMappingToObject(typeof(ProtocolType[]), new ConvertMethodHandler(Oct.ToProtocol));
            #endregion

            #region ToString

            SetMappingToString(SystemTypes.Boolean, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.BooleanArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Byte, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.ByteArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Char, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.CharArray, new ConvertToStringMethodHandler(Oct.ToString));
#if !SILVERLIGHT
            SetMappingToString(SystemTypes.Color, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.ColorArray, new ConvertToStringMethodHandler(Oct.ToString));
#endif
            SetMappingToString(SystemTypes.DateTime, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.DateTimeArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Decimal, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.DecimalArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Double, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.DoubleArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Int16, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Int16Array, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Int32, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Int32Array, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Int64, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Int64Array, new ConvertToStringMethodHandler(Oct.ToString));
#if !SILVERLIGHT
            SetMappingToString(SystemTypes.Point, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.PointArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.PointF, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.PointFArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Rectangle, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.RectangleArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.RectangleF, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.RectangleFArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Size, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.SizeArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.SizeF, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.SizeFArray, new ConvertToStringMethodHandler(Oct.ToString));
#endif
            SetMappingToString(SystemTypes.SByte, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.SByteArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.Single, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.SingleArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.String, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.StringArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.TimeSpan, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.TimeSpanArray, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.UInt16, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.UInt16Array, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.UInt32, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.UInt32Array, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.UInt64, new ConvertToStringMethodHandler(Oct.ToString));
            SetMappingToString(SystemTypes.UInt64Array, new ConvertToStringMethodHandler(Oct.ToString));

            #endregion
        }