Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 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);
        }