Exemple #1
0
 /// <summary>
 /// Determine if the Input value can be converted to TargetType
 /// </summary>
 public static bool CanConvertTo(this object Input, Type TargetType)
 {
     if (Input == null || Input.GetType().IsAssignableFrom(TargetType) || TargetType.IsAssignableFrom(Input.GetType()) || TargetType == typeof(string))
     {
         return(true);
     }
     return(Utility.GetConverter(Input.GetType()).CanConvertTo(TargetType) ||
            Utility.GetConverter(TargetType).CanConvertFrom(Input.GetType()) ||
            (TargetType.IsEnumType() && Input.GetType().IsEnumType()));
 }
Exemple #2
0
        /// <summary>
        /// Convert value to specific T type
        /// </summary>
        public static object ConvertTo(this object Input, Type TargetType, OnErrorAction OnErrorAction = OnErrorAction.ThrowException)
        {
            if (Input == null || TargetType.IsAssignableFrom(Input.GetType()))
            {
                return(Input);
            }
            if (TargetType == typeof(string))
            {
                try { return(Utility.GetConverter(Input.GetType()).ConvertTo(Input, TargetType)); }
                catch { return(Input.ToString()); }
            }

            if (!CanConvertTo(Input, TargetType))
            {
                if (OnErrorAction == OnErrorAction.ThrowException)
                {
                    throw new Exception("Can't convert type " + Input.GetType().Name + " to type " + TargetType.Name);
                }
                else if (OnErrorAction == OnErrorAction.ReturnNull)
                {
                    return(Utility.GetDefault(TargetType));
                }
                else
                {
                    return(Input);
                }
            }
            try
            {
                if (TargetType.IsEnumType() && Input.GetType().IsEnumType())
                {
                    Input = Input + "";
                }
                try { return(Utility.GetConverter(TargetType).ConvertFrom(Input)); }
                catch (Exception ex)
                {
                    if (TargetType == typeof(string) && Input is IList)
                    {
                        return(Utility.SerializeArrayAsString(Input as IList));
                    }
                    if (TargetType.IsList() && Input.GetType() == typeof(string))
                    {
                        return(Utility.DeSerializeArrayFromString(TargetType, Input as string));
                    }
                    throw ex;
                }
            }
            catch
            {
                try { return(Utility.GetConverter(Input.GetType()).ConvertTo(Input, TargetType)); }
                catch
                {
                    if (OnErrorAction == OnErrorAction.ThrowException)
                    {
                        throw;
                    }
                    else if (OnErrorAction == OnErrorAction.ReturnNull)
                    {
                        return(Utility.GetDefault(TargetType));
                    }
                    else
                    {
                        return(Input);
                    }
                }
            }
        }