Пример #1
0
        public static T AppSetting <T>(string key, T defaultValue = default(T)) where T : struct
        {
            var result = default(T);
            var type   = typeof(T);
            var value  = ConfigurationManager.AppSettings[key];

            if (type.IsEnum)
            {
                if (value.IsNumeric())
                {
                    result = SafeClrConvert.ToInt(value).ToEnum(defaultValue);
                }
                else
                {
                    if (!Enum.TryParse(value, out result))
                    {
                        result = defaultValue;
                    }
                }
            }
            else
            {
                if (type.IsBasicType())
                {
                    do
                    {
                        if (type == TypeHelper.TypeOfBool)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToBoolean(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfInt16)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToInt16(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfInt32)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToInt32(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfInt64)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToInt64(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfByte)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToByte(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfSByte)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToSByte(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfUInt16)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToUInt16(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfUInt32)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToUInt32(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfUInt64)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToUInt64(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfChar)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToChar(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfDateTime)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToDateTime(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfDecimal)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToDecimal(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfDouble)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToDouble(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfFloat)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToSingle(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfGuid)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToGuid(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfTimeSpan)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToTimeSpan(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfString)
                        {
                            result = (T)((object)value);
                            break;
                        }
                    }while (false);
                }
            }

            return(result);
        }
Пример #2
0
        public static T ReflectionSafeTransform <T>(IDataReader reader) where T : class, new()
        {
            var result = new T();

            if (!reader.IsClosed)
            {
                var props = GlobalReflectionPropertyCache.Cache.GetPublicInstanceReadableProperties <T>();

                for (var i = 0; i < reader.FieldCount; i++)
                {
                    if (!reader.IsDBNull(i))
                    {
                        var column = reader.GetName(i);
                        var value  = reader[i];

                        var prop = props.FirstOrDefault(p => string.Compare(p.Name, column, true) == 0);

                        if (prop != null)
                        {
                            try
                            {
                                if (prop.PropertyType.IsNullable())
                                {
                                    if (value != null)
                                    {
                                        object nullableValue  = null;
                                        object convertedValue = null;

                                        var nullableFinalType = prop.PropertyType.GenericTypeArguments[0];

                                        if (nullableFinalType == TypeHelper.TypeOfByte)
                                        {
                                            convertedValue = SafeClrConvert.ToByte(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfInt16)
                                        {
                                            convertedValue = SafeClrConvert.ToInt16(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfInt32)
                                        {
                                            convertedValue = SafeClrConvert.ToInt32(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfInt64)
                                        {
                                            convertedValue = SafeClrConvert.ToInt64(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfSByte)
                                        {
                                            convertedValue = SafeClrConvert.ToSByte(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfUInt16)
                                        {
                                            convertedValue = SafeClrConvert.ToUInt16(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfUInt32)
                                        {
                                            convertedValue = SafeClrConvert.ToUInt32(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfUInt64)
                                        {
                                            convertedValue = SafeClrConvert.ToUInt64(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfSingle)
                                        {
                                            convertedValue = SafeClrConvert.ToSingle(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfDouble)
                                        {
                                            convertedValue = SafeClrConvert.ToDouble(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfDecimal)
                                        {
                                            convertedValue = SafeClrConvert.ToDecimal(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfString)
                                        {
                                            convertedValue = SafeClrConvert.ToString(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfDateTime)
                                        {
                                            convertedValue = SafeClrConvert.ToDateTime(value);
                                        }
                                        else
                                        if (nullableFinalType == TypeHelper.TypeOfBool)
                                        {
                                            convertedValue = SafeClrConvert.ToBoolean(value);
                                        }

                                        nullableValue = Activator.CreateInstance(prop.PropertyType, new object[] { convertedValue });

                                        prop.SetValue(result, nullableValue);
                                    }
                                }
                                else
                                {
                                    object convertedValue = null;

                                    var propType = prop.PropertyType;

                                    if (propType == TypeHelper.TypeOfByte)
                                    {
                                        convertedValue = SafeClrConvert.ToByte(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfInt16)
                                    {
                                        convertedValue = SafeClrConvert.ToInt16(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfInt32)
                                    {
                                        convertedValue = SafeClrConvert.ToInt32(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfInt64)
                                    {
                                        convertedValue = SafeClrConvert.ToInt64(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfSByte)
                                    {
                                        convertedValue = SafeClrConvert.ToSByte(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfUInt16)
                                    {
                                        convertedValue = SafeClrConvert.ToUInt16(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfUInt32)
                                    {
                                        convertedValue = SafeClrConvert.ToUInt32(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfUInt64)
                                    {
                                        convertedValue = SafeClrConvert.ToUInt64(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfSingle)
                                    {
                                        convertedValue = SafeClrConvert.ToSingle(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfDouble)
                                    {
                                        convertedValue = SafeClrConvert.ToDouble(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfDecimal)
                                    {
                                        convertedValue = SafeClrConvert.ToDecimal(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfString)
                                    {
                                        convertedValue = SafeClrConvert.ToString(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfDateTime)
                                    {
                                        convertedValue = SafeClrConvert.ToDateTime(value);
                                    }
                                    else
                                    if (propType == TypeHelper.TypeOfBool)
                                    {
                                        convertedValue = SafeClrConvert.ToBoolean(value);
                                    }

                                    if (convertedValue != null)
                                    {
                                        prop.SetValue(result, convertedValue);
                                    }
                                    else
                                    {
                                        prop.SetValue(result, value);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                throw new Exception($"error reading column {column} into prop {prop.Name}", e);
                            }
                        }
                    }
                }
            }

            return(result);
        }
Пример #3
0
        public object Map(IDataReader reader, Type type)
        {
            var result = Activator.CreateInstance(type);

            ReflectionHelper.ForEachPublicInstanceReadableProperty(type, prop =>
            {
                if (reader.HasColumn(prop.Name))
                {
                    var value = reader[prop.Name];

                    if (value != null && !DBNull.Value.Equals(value))
                    {
                        var propType = prop.PropertyType;

                        if (propType == TypeHelper.TypeOfBool || propType == TypeHelper.TypeOfNullableBool)
                        {
                            prop.SetValue(result, SafeClrConvert.ToBoolean(value));
                        }
                        else if (propType == TypeHelper.TypeOfByte || propType == TypeHelper.TypeOfNullableByte)
                        {
                            prop.SetValue(result, SafeClrConvert.ToByte(value));
                        }
                        else if (propType == TypeHelper.TypeOfByteArray)
                        {
                            prop.SetValue(result, (byte[])value);
                        }
                        else if (propType == TypeHelper.TypeOfChar || propType == TypeHelper.TypeOfNullableChar)
                        {
                            prop.SetValue(result, SafeClrConvert.ToChar(value));
                        }
                        else if (propType == TypeHelper.TypeOfDateTime || propType == TypeHelper.TypeOfNullableDateTime)
                        {
                            prop.SetValue(result, SafeClrConvert.ToDateTime(value));
                        }
                        else if (propType == TypeHelper.TypeOfDateTimeOffset || propType == TypeHelper.TypeOfNullableDateTimeOffset)
                        {
                            prop.SetValue(result, SafeClrConvert.ToDateTime(value));
                        }
                        else if (propType == TypeHelper.TypeOfDecimal || propType == TypeHelper.TypeOfNullableDecimal)
                        {
                            prop.SetValue(result, SafeClrConvert.ToDecimal(value));
                        }
                        else if (propType == TypeHelper.TypeOfDouble || propType == TypeHelper.TypeOfNullableDouble)
                        {
                            prop.SetValue(result, SafeClrConvert.ToDouble(value));
                        }
                        else if (propType == TypeHelper.TypeOfFloat || propType == TypeHelper.TypeOfNullableFloat)
                        {
                            prop.SetValue(result, SafeClrConvert.ToSingle(value));
                        }
                        else if (propType == TypeHelper.TypeOfGuid)
                        {
                            prop.SetValue(result, new Guid(SafeClrConvert.ToString(value)));
                        }
                        else if (propType == TypeHelper.TypeOfInt16 || propType == TypeHelper.TypeOfNullableInt16)
                        {
                            prop.SetValue(result, SafeClrConvert.ToInt16(value));
                        }
                        else if (propType == TypeHelper.TypeOfInt32 || propType == TypeHelper.TypeOfNullableInt32)
                        {
                            prop.SetValue(result, SafeClrConvert.ToInt32(value));
                        }
                        else if (propType == TypeHelper.TypeOfInt64 || propType == TypeHelper.TypeOfNullableInt64)
                        {
                            prop.SetValue(result, SafeClrConvert.ToInt64(value));
                        }
                        else if (propType == TypeHelper.TypeOfSByte || propType == TypeHelper.TypeOfNullableSByte)
                        {
                            prop.SetValue(result, SafeClrConvert.ToSByte(value));
                        }
                        else if (propType == TypeHelper.TypeOfString)
                        {
                            prop.SetValue(result, SafeClrConvert.ToString(value));
                        }
                        else if (propType == TypeHelper.TypeOfTimeSpan || propType == TypeHelper.TypeOfNullableTimeSpan)
                        {
                            prop.SetValue(result, SafeClrConvert.ToDateTime(value));
                        }
                        else if (propType == TypeHelper.TypeOfUInt16 || propType == TypeHelper.TypeOfNullableUInt16)
                        {
                            prop.SetValue(result, SafeClrConvert.ToUInt16(value));
                        }
                        else if (propType == TypeHelper.TypeOfUInt32 || propType == TypeHelper.TypeOfNullableUInt32)
                        {
                            prop.SetValue(result, SafeClrConvert.ToUInt32(value));
                        }
                        else if (propType == TypeHelper.TypeOfUInt64 || propType == TypeHelper.TypeOfNullableUInt64)
                        {
                            prop.SetValue(result, SafeClrConvert.ToUInt64(value));
                        }
                    }
                }
            });

            return(result);
        }
Пример #4
0
        public static void SafeApplyOutputs(this DbCommand cmd, object parameters, PropertyInfo[] properties = null)
        {
            cmd.ApplyOutputs(parameters, (obj, prop, value) =>
            {
                if (value == null || DBNull.Value.Equals(value))
                {
                    if (prop.PropertyType.IsNullable() && prop.CanWrite)
                    {
                        prop.SetValue(obj, null);
                    }
                }
                else
                {
                    if (prop.PropertyType == typeof(CommandParameter))
                    {
                        var cp = prop.GetValue(obj) as CommandParameter;

                        if (cp != null)
                        {
                            cp.Value = value;
                        }
                    }
                    else
                    {
                        do
                        {
                            if (prop.PropertyType == TypeHelper.TypeOfInt16 || prop.PropertyType == TypeHelper.TypeOfNullableInt16)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToInt16(value));
                                break;
                            }


                            if (prop.PropertyType == TypeHelper.TypeOfInt32 || prop.PropertyType == TypeHelper.TypeOfNullableInt32)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToInt32(value));
                                break;
                            }


                            if (prop.PropertyType == TypeHelper.TypeOfInt64 || prop.PropertyType == TypeHelper.TypeOfNullableInt64)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToInt64(value));
                                break;
                            }


                            if (prop.PropertyType == TypeHelper.TypeOfUInt16 || prop.PropertyType == TypeHelper.TypeOfNullableUInt16)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToUInt16(value));
                                break;
                            }


                            if (prop.PropertyType == TypeHelper.TypeOfUInt32 || prop.PropertyType == TypeHelper.TypeOfNullableUInt32)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToUInt32(value));
                                break;
                            }


                            if (prop.PropertyType == TypeHelper.TypeOfUInt64 || prop.PropertyType == TypeHelper.TypeOfNullableUInt64)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToUInt64(value));
                                break;
                            }


                            if (prop.PropertyType == TypeHelper.TypeOfSingle || prop.PropertyType == TypeHelper.TypeOfNullableSingle)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToSingle(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfFloat || prop.PropertyType == TypeHelper.TypeOfNullableFloat)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToFloat(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfDouble || prop.PropertyType == TypeHelper.TypeOfNullableDouble)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToDouble(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfDecimal || prop.PropertyType == TypeHelper.TypeOfNullableDecimal)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToDecimal(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfByte || prop.PropertyType == TypeHelper.TypeOfNullableByte)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToByte(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfSByte || prop.PropertyType == TypeHelper.TypeOfNullableSByte)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToSByte(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfChar || prop.PropertyType == TypeHelper.TypeOfNullableChar)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToChar(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfString)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToString(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfBool || prop.PropertyType == TypeHelper.TypeOfNullableBool)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToBoolean(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfDateTime || prop.PropertyType == TypeHelper.TypeOfNullableDateTime)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToDateTime(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfDateTimeOffset || prop.PropertyType == TypeHelper.TypeOfNullableDateTimeOffset)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToDateTime(value));
                                break;
                            }

                            if (prop.PropertyType == TypeHelper.TypeOfTimeSpan || prop.PropertyType == TypeHelper.TypeOfNullableTimeSpan)
                            {
                                prop.SetValue(obj, SafeClrConvert.ToTimeSpan(value));
                                break;
                            }

                            prop.SetValue(obj, value);
                        }while (false);
                    }
                }
            }, properties);
        }
Пример #5
0
 public static System.SByte ToSByte(this string s, System.SByte @default = default(System.SByte))
 {
     return(SafeClrConvert.ToSByte(s, @default));
 }