Esempio 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);
        }