コード例 #1
0
        private static VLTBaseType DoPrimitiveConversion(PrimitiveTypeBase primitiveTypeBase, string str, Type conversionType)
        {
            if (conversionType.IsEnum)
            {
                if (str.StartsWith("0x") &&
                    uint.TryParse(str.Substring(2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out uint val))
                {
                    primitiveTypeBase.SetValue((IConvertible)Enum.Parse(conversionType, val.ToString()));
                }
                else
                {
                    primitiveTypeBase.SetValue((IConvertible)Enum.Parse(conversionType, str));
                }
            }
            else
            {
                if (str.StartsWith("0x") && uint.TryParse(str.Substring(2), NumberStyles.AllowHexSpecifier,
                                                          CultureInfo.InvariantCulture, out uint val))
                {
                    primitiveTypeBase.SetValue((IConvertible)Convert.ChangeType(val, conversionType));
                }
                else
                {
                    primitiveTypeBase.SetValue(
                        (IConvertible)Convert.ChangeType(str, conversionType, CultureInfo.InvariantCulture));
                }
            }

            return(primitiveTypeBase);
        }
コード例 #2
0
        private static VLTBaseType DoPrimitiveConversion(PrimitiveTypeBase primitiveTypeBase, string str, Type conversionType)
        {
            if (conversionType.IsEnum)
            {
                if (str.StartsWith("0x") &&
                    uint.TryParse(str.Substring(2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out uint val))
                {
                    primitiveTypeBase.SetValue((IConvertible)Enum.Parse(conversionType, val.ToString()));
                }
                else
                {
                    primitiveTypeBase.SetValue((IConvertible)Enum.Parse(conversionType, str));
                }
            }
            else
            {
                if (str.StartsWith("0x") && uint.TryParse(str.Substring(2), NumberStyles.AllowHexSpecifier,
                                                          CultureInfo.InvariantCulture, out uint val))
                {
                    primitiveTypeBase.SetValue((IConvertible)Convert.ChangeType(val, conversionType));
                }
                else
                {
                    try
                    {
                        primitiveTypeBase.SetValue(
                            (IConvertible)Convert.ChangeType(str, conversionType, CultureInfo.InvariantCulture));
                    }
                    catch (Exception e)
                    {
                        throw new SerializedDatabaseLoaderException($"Failed to parse value [{str}] as type {conversionType}", e);
                    }
                }
            }

            return(primitiveTypeBase);
        }