コード例 #1
0
        internal static object InternalGetDefaultValue(Type type, string strValue, object defaultValue,
                                                       ReadSettings settings, bool throwException)
        {
            ITkTypeConverter converter = TkTypeDescriptor.GetConverter(type);

            if (throwException)
            {
                TkDebug.AssertNotNull(converter, string.Format(ObjectUtil.SysCulture,
                                                               "无法获取类型{0}的TypeConverter,请确认是否为其配置TypeConverterAttribute",
                                                               type), null);
            }
            else
            {
                if (converter == null)
                {
                    return(GetTypeDefaultValue(type));
                }
            }
            try
            {
                return(strValue == null?InternalGetDefaultValue(type, defaultValue)
                           : converter.ConvertFromString(strValue, settings));
            }
            catch
            {
                return(InternalGetDefaultValue(type, defaultValue));
            }
        }
コード例 #2
0
 internal static object InternalGetValue(Type type, string strValue,
                                         object defaultValue, ReadSettings settings, ITkTypeConverter converter)
 {
     try
     {
         return(strValue == null?GetDefaultValue(type, defaultValue, converter, settings)
                    : converter.ConvertFromString(strValue, settings));
     }
     catch
     {
         return(GetDefaultValue(type, defaultValue, converter, settings));
     }
 }
コード例 #3
0
        public object ConvertFromString(string text, ReadSettings settings)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            object         result = fConverter.ConvertFromString(text, settings);
            IConvertStatus status = fConverter as IConvertStatus;

            if (status != null && !status.IsSuccess)
            {
                return(null);
            }
            return(result);
        }
コード例 #4
0
 private static object GetDefaultValue(Type type, object defaultValue,
                                       ITkTypeConverter converter, ReadSettings settings)
 {
     if (defaultValue != null)
     {
         if (ObjectUtil.IsSubType(type, defaultValue.GetType()))
         {
             return(defaultValue);
         }
         else
         {
             try
             {
                 string value = defaultValue.ToString();
                 return(converter.ConvertFromString(value, settings));
             }
             catch
             {
                 return(null);
             }
         }
     }
     else
     {
         if (type.IsEnum)
         {
             return(GetFirstEnumValue(type));
         }
         else if (type.IsValueType)
         {
             return(ObjectUtil.CreateObject(type));
         }
         else
         {
             return(null);
         }
     }
 }