예제 #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));
        }
예제 #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));
 }
예제 #3
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);
        }
예제 #4
0
 public static object To(this string value, Type type)
 {
     return(TypeSerializer.DeserializeFromString(value, type));
 }
예제 #5
0
 public static T ToOrDefaultValue <T>(this string value)
 {
     return(String.IsNullOrEmpty(value) ? default(T) : TypeSerializer.DeserializeFromString <T>(value));
 }
예제 #6
0
 public static T To <T>(this string value)
 {
     return(TypeSerializer.DeserializeFromString <T>(value));
 }