Example #1
0
        private static object ChangeValueType(object from, Type type)
        {
            if (from is string strValue)
            {
                return(SSTTypeSerializer.DeserializeFromString(strValue, type));
            }

            if (type == typeof(string))
            {
                return(from.ToJsv());
            }

            return(Convert.ChangeType(from, type, provider: null));
        }
Example #2
0
 public static object ChangeTo(this string strValue, Type type)
 {
     if (type.IsValueType && !type.IsEnum && type.HasInterface(typeof(IConvertible)))
     {
         try
         {
             return(Convert.ChangeType(strValue, type, provider: null));
         }
         catch (Exception ex)
         {
             Tracer.Instance.WriteError(ex);
         }
     }
     return(SSTTypeSerializer.DeserializeFromString(strValue, type));
 }
Example #3
0
 public object DeserializeFromString(string serializedText, Type type)
 {
     return(TypeSerializer.DeserializeFromString(serializedText, type));
 }
Example #4
0
 public To DeserializeFromString <To>(string serializedText)
 {
     return(TypeSerializer.DeserializeFromString <To>(serializedText));
 }
 public static T FromJsv <T>(this string jsv)
 {
     return(TypeSerializer.DeserializeFromString <T>(jsv));
 }
 public static object To(this string value, Type type)
 {
     return(TypeSerializer.DeserializeFromString(value, type));
 }
 public static T ToOrDefaultValue <T>(this string value)
 {
     return(string.IsNullOrEmpty(value) ? default(T) : TypeSerializer.DeserializeFromString <T>(value));
 }
 public static T To <T>(this string value)
 {
     return(TypeSerializer.DeserializeFromString <T>(value));
 }
Example #9
0
        public static MemberGetter CreateTypeConverter(Type fromType, Type toType)
        {
            if (fromType == toType)
            {
                return(null);
            }

            if (fromType == typeof(string))
            {
                return(fromValue => SSTTypeSerializer.DeserializeFromString((string)fromValue, toType));
            }

            if (toType == typeof(string))
            {
                return(SSTTypeSerializer.SerializeToString);
            }

            var underlyingToType   = Nullable.GetUnderlyingType(toType) ?? toType;
            var underlyingFromType = Nullable.GetUnderlyingType(fromType) ?? fromType;

            if (underlyingToType.IsEnum)
            {
                if (underlyingFromType.IsEnum || fromType == typeof(string))
                {
                    return(fromValue => Enum.Parse(underlyingToType, fromValue.ToString(), ignoreCase: true));
                }

                if (underlyingFromType.IsIntegerType())
                {
                    return(fromValue => Enum.ToObject(underlyingToType, fromValue));
                }
            }
            else if (underlyingFromType.IsEnum)
            {
                if (underlyingToType.IsIntegerType())
                {
                    return(fromValue => Convert.ChangeType(fromValue, underlyingToType, null));
                }
            }
            else if (typeof(IEnumerable).IsAssignableFrom(fromType))
            {
                return(fromValue =>
                {
                    var listResult = TranslateListWithElements.TryTranslateCollections(
                        fromType, underlyingToType, fromValue);

                    return listResult ?? fromValue;
                });
            }
            else if (underlyingToType.IsValueType)
            {
                return(fromValue => Convert.ChangeType(fromValue, underlyingToType, provider: null));
            }
            else
            {
                return(fromValue =>
                {
                    if (fromValue == null)
                    {
                        return fromValue;
                    }

                    var toValue = toType.CreateInstance();
                    toValue.PopulateWith(fromValue);
                    return toValue;
                });
            }

            return(null);
        }