Example #1
0
 public LightDataTableShared()
 {
     Columns             = new ColumnsCollections <string>();
     ColumnsWithIndexKey = new ColumnsCollections <int>();
     RoundingSettings    = new RoundingSettings();
     Culture             = GlobalConfiguration.CultureInfo;
     ValidateCulture();
 }
Example #2
0
        protected void TypeValidation(ref object value, Type dataType, bool loadDefaultOnError, object defaultValue = null)
        {
            try
            {
                ValidateCulture();
                if (value == null || value is DBNull)
                {
                    value = ValueByType(dataType, defaultValue);
                    return;
                }

                if (IgnoreTypeValidation)
                {
                    return;
                }

                if (dataType == typeof(byte[]) && value.GetType() == typeof(string))
                {
                    if (value.ToString().Length % 4 == 0) // its a valid base64string
                    {
                        value = Convert.FromBase64String(value.ToString());
                    }
                    return;
                }

                if (dataType == typeof(int?) || dataType == typeof(int))
                {
                    if (double.TryParse(CleanValue(dataType, value).ToString(), NumberStyles.Float, Culture, out var douTal))
                    {
                        value = Convert.ToInt32(douTal);
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }

                    return;
                }

                if (dataType == typeof(long?) || dataType == typeof(long))
                {
                    if (double.TryParse(CleanValue(dataType, value).ToString(), NumberStyles.Float, Culture, out var douTal))
                    {
                        value = Convert.ToInt64(douTal);
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }

                    return;
                }

                if (dataType == typeof(float?) || dataType == typeof(float))
                {
                    if (float.TryParse(CleanValue(dataType, value).ToString(), NumberStyles.Float, Culture, out var douTal))
                    {
                        value = RoundingSettings.Round(douTal);
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }

                    return;
                }

                if (dataType == typeof(decimal?) || dataType == typeof(decimal))
                {
                    if (decimal.TryParse(CleanValue(dataType, value).ToString(), NumberStyles.Float, Culture, out var decTal))
                    {
                        value = RoundingSettings.Round(decTal);
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType == typeof(double?) || dataType == typeof(double))
                {
                    if (double.TryParse(CleanValue(dataType, value).ToString(), NumberStyles.Float, Culture, out var douTal))
                    {
                        value = RoundingSettings.Round(douTal);
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType == typeof(DateTime?) || dataType == typeof(DateTime))
                {
                    if (DateTime.TryParse(value.ToString(), Culture, DateTimeStyles.None, out var dateValue))
                    {
                        if (dateValue < SqlDateTime.MinValue)
                        {
                            dateValue = SqlDateTime.MinValue.Value;
                        }
                        value = dateValue;
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType == typeof(bool?) || dataType == typeof(bool))
                {
                    if (bool.TryParse(value.ToString(), out var boolValue))
                    {
                        value = boolValue;
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType == typeof(TimeSpan?) || dataType == typeof(TimeSpan))
                {
                    if (TimeSpan.TryParse(value.ToString(), Culture, out var timeSpanValue))
                    {
                        value = timeSpanValue;
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType.IsEnum || (dataType.GenericTypeArguments?.FirstOrDefault()?.IsEnum ?? false))
                {
                    var type = dataType;
                    if ((dataType.GenericTypeArguments?.FirstOrDefault()?.IsEnum ?? false))
                    {
                        type = (dataType.GenericTypeArguments?.FirstOrDefault());
                    }
                    if (value is int || value is long)
                    {
                        if (Enum.IsDefined(type, Convert.ToInt32(value)))
                        {
                            value = Enum.ToObject(type, Convert.ToInt32(value));
                        }
                    }
                    else if (Enum.IsDefined(type, value))
                    {
                        value = Enum.Parse(type, value.ToString(), true);
                    }
                    else if (loadDefaultOnError)
                    {
                        value = Activator.CreateInstance(dataType);
                    }
                }
                else if (dataType == typeof(Guid) || dataType == typeof(Guid?))
                {
                    if (Guid.TryParse(value.ToString(), out Guid v))
                    {
                        value = v;
                    }
                    else if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                }
                else if (dataType == typeof(string))
                {
                    value = value.ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Error: InvalidType. ColumnType is {dataType.FullName} and the given value is of type {value.GetType().FullName} Original Exception {ex.Message}");
            }
        }
Example #3
0
        protected void TypeValidation(ref object value, Type dataType, bool loadDefaultOnError, object defaultValue = null)
        {
            try
            {
                ValidateCulture();
                if (value == null || value is DBNull)
                {
                    value = ValueByType(dataType, defaultValue);
                    return;
                }

                if (IgnoreTypeValidation)
                {
                    return;
                }

                if (dataType == typeof(int?) || dataType == typeof(int))
                {
                    if (double.TryParse(CleanValue(dataType, value).ToString(), NumberStyles.Float, Culture, out var douTal))
                    {
                        value = Convert.ToInt32(douTal);
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }

                    return;
                }

                if (dataType == typeof(long?) || dataType == typeof(long))
                {
                    if (double.TryParse(CleanValue(dataType, value).ToString(), NumberStyles.Float, Culture, out var douTal))
                    {
                        value = Convert.ToInt64(douTal);
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }

                    return;
                }

                if (dataType == typeof(decimal?) || dataType == typeof(decimal))
                {
                    if (decimal.TryParse(CleanValue(dataType, value).ToString(), NumberStyles.Float, Culture, out var decTal))
                    {
                        value = RoundingSettings.Round(decTal);
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType == typeof(double?) || dataType == typeof(double))
                {
                    if (double.TryParse(CleanValue(dataType, value).ToString(), NumberStyles.Float, Culture, out var douTal))
                    {
                        value = RoundingSettings.Round(douTal);
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType == typeof(DateTime?) || dataType == typeof(DateTime))
                {
                    if (DateTime.TryParse(value.ToString(), Culture, DateTimeStyles.None, out var dateValue))
                    {
                        value = dateValue;
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType == typeof(bool?) || dataType == typeof(bool))
                {
                    if (bool.TryParse(value.ToString(), out var boolValue))
                    {
                        value = boolValue;
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType == typeof(TimeSpan?) || dataType == typeof(TimeSpan))
                {
                    if (TimeSpan.TryParse(value.ToString(), Culture, out var timeSpanValue))
                    {
                        value = timeSpanValue;
                    }
                    else
                    if (loadDefaultOnError)
                    {
                        value = ValueByType(dataType, defaultValue);
                    }
                    return;
                }

                if (dataType.IsEnum)
                {
                    if (value is int || value is long)
                    {
                        if (Enum.IsDefined(dataType, Convert.ToInt32(value)))
                        {
                            value = Enum.ToObject(dataType, Convert.ToInt32(value));
                        }
                    }
                    else if (Enum.IsDefined(dataType, value))
                    {
                        value = Enum.Parse(dataType, value.ToString(), true);
                    }
                }
                else if (dataType == typeof(Guid) || dataType == typeof(Guid?))
                {
                    if (Guid.TryParse(value.ToString(), out Guid v))
                    {
                        value = v;
                    }
                }
                else if (dataType == typeof(string))
                {
                    value = value.ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: InvalidType. ColumnType is " + dataType.FullName + " and the given value is of type " + value.GetType().FullName + " Original Exception " + ex.Message);
            }
        }