public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { if (iniContext.TypeDetailsInformation.UnderlyingKeyTypeInformation.TypeCode == TypeCode.ComplexObject || iniContext.TypeDetailsInformation.UnderlyingTypeInformation.TypeCode == TypeCode.ComplexObject) { throw new CollectionOfComplexTypeException($"Collection of complex type not supported for {iniContext}"); } if (!(objectToFormat is IDictionary dictionary)) { throw new InvalidOperationException(); } var dictionaryEnumerator = dictionary.GetEnumerator(); while (dictionaryEnumerator.MoveNext()) { if (dictionaryEnumerator.Key == null || dictionaryEnumerator.Value == null) { continue; } var value = _underlyingTypeIniConverter.FormatToWrite(dictionaryEnumerator.Value, iniContext)?.Value; iniContext.IniParser.Write(iniContext.IniValue.Key, _underlyingKeyTypeIniConverter.FormatToWrite(dictionaryEnumerator.Key, iniContext)?.Value, value); } return(null); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { var dateTime = (DateTime)objectToFormat; iniContext.IniValue.Value = dateTime.ToString("O"); return(iniContext.IniValue); }
public object ReadValue(IMemberInfoWrapper memberInfoWrapper, object configuration, Type configurationType) { var(converter, defaultConverter, typeDetailsInformation) = _iniConverterFactory.GetConverter(memberInfoWrapper.GetMemberType(), 0, memberInfoWrapper); if (typeDetailsInformation.TypeCode == TypeCode.ComplexObject) { return(converter.ParseReadValue(null, typeDetailsInformation.Type, null)); } var iniValue = new IniValue() { Section = _iniValueManager.GetSection(configurationType, memberInfoWrapper), Key = _iniValueManager.GetKey(memberInfoWrapper) }; try { var iniContext = new IniContext(memberInfoWrapper, typeDetailsInformation, iniValue, _iniParser, defaultConverter); var readValue = _iniParser.Read(iniValue.Section, iniValue.Key); return(converter.ParseReadValue(readValue, typeDetailsInformation.Type, iniContext)); } catch (FormatException) { throw new IniWrongFormatException($"Wrong format in {iniValue} expected type: {memberInfoWrapper.GetMemberType()}"); } }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { var listType = typeof(List <>).MakeGenericType(iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type); var returnedList = (IList)Activator.CreateInstance(listType); for (int i = 0; i < int.MaxValue; i++) { var dynamicIniOptionsAttribute = new IniOptionsAttribute() { Section = GenerateDynamicSection(iniContext, i) }; var section = iniContext.IniParser.Read(dynamicIniOptionsAttribute.Section, null); if (string.IsNullOrEmpty(section)) { break; } var loadedComplexType = _iniWrapperWithCustomMemberInfo.LoadConfigurationFromFileWithCustomMemberInfo( iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type, dynamicIniOptionsAttribute); returnedList.Add(loadedComplexType); } return(returnedList); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { if (string.IsNullOrEmpty(readValue)) { return(null); } return(Enum.Parse(destinationType, readValue)); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { var castedValue = (bool)objectToFormat; iniContext.IniValue.Value = castedValue ? TrueValue : FalseValue; return(iniContext.IniValue); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { var castedUnderlyingType = ToUInt64(objectToFormat); iniContext.IniValue.Value = castedUnderlyingType.ToString(); return(iniContext.IniValue); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { if (string.IsNullOrEmpty(readValue)) { return(null); } return(Convert.ChangeType(readValue, destinationType)); }
public void ParseReadValue_ShouldParseInt(Type type, string parsingValue, object expected) { var iniContext = new IniContext(null, new TypeDetailsInformation( TypeCode.BigInteger, null, null, type), null, null, null); var result = _primitivesConverter.ParseReadValue(parsingValue, type, iniContext); result.Should().Be(expected); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { readValue = iniContext.IniParser.Read(iniContext.IniValue.Key, null); var splitReadValues = _readSectionsParser.Parse(readValue); var returnedDictionary = CreateDictionary(iniContext); foreach (var splitReadValue in splitReadValues) { var key = _underlyingKeyTypeIniConverter.ParseReadValue(splitReadValue.Key, iniContext.TypeDetailsInformation.UnderlyingKeyTypeInformation.Type, iniContext); var value = _underlyingTypeIniConverter.ParseReadValue(splitReadValue.Value, iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type, iniContext); returnedDictionary.Add(key, value); } return(returnedDictionary); }
/// <summary> /// 读取或者创建ini /// </summary> /// <param name="path">文件路径</param> /// <returns></returns> public static async Task <IniContext> ReadOrCreateAsync(string path) { FileInfo fileInfo = new FileInfo(path); IniContext iniCotext = new IniContext() { Sections = new IniContext.Content() { Items = new List <IniContext.Section>() }, File = fileInfo }; string[] lines = null; if (File.Exists(path)) { lines = await File.ReadAllLinesAsync(path); } else { File.WriteAllText(path, string.Empty); return(iniCotext); } IniContext.Section tempSection = null; try { for (int i = 0; i < lines?.Length; i++) { string item = lines[i].TrimStart(); //; ;为注解 if (item.IndexOf(';') == 0 || item.IndexOf(';') == 0 || string.IsNullOrEmpty(item)) { continue; } NextSection: if (tempSection == null) { int sectionStart = item.IndexOf('['); int sectionEnd = item.LastIndexOf(']'); if (sectionStart == 0 && sectionEnd >= 1) { tempSection = new IniContext.Section { Name = item[(sectionStart + 1)..sectionEnd],
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { if (string.IsNullOrEmpty(readValue)) { return(null); } var genericType = iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type; if (iniContext.TypeDetailsInformation.UnderlyingTypeInformation.TypeCode == TypeCode.Nullable) { genericType = typeof(Nullable <>).MakeGenericType(iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type); } var listType = typeof(List <>).MakeGenericType(genericType); var returnedList = (IList)Activator.CreateInstance(listType); foreach (var singleEntity in readValue.Split(new[] { _iniSettings.EnumerableEntitySeparator }, StringSplitOptions.RemoveEmptyEntries)) { returnedList.Add(_underlyingTypeIniConverter.ParseReadValue(singleEntity, iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type, iniContext)); } return(returnedList); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { var enumerable = objectToFormat as IEnumerable; var stringBuilder = new StringBuilder(); foreach (var item in enumerable) { if (item == null) { continue; } stringBuilder.Append(_underlyingTypeIniConverter.FormatToWrite(item, iniContext)?.Value); stringBuilder.Append(_iniSettings.EnumerableEntitySeparator); } RemoveLastSeparator(stringBuilder); iniContext.IniValue.Value = stringBuilder.ToString(); return(iniContext.IniValue); }
public void SaveValue(IMemberInfoWrapper memberInfoWrapper, object configuration) { var value = memberInfoWrapper.GetValue(configuration); var defaultIniValue = new IniValue() { Section = _iniValueManager.GetSection(configuration.GetType(), memberInfoWrapper), Key = _iniValueManager.GetKey(memberInfoWrapper), }; var(converter, defaultConverter, typeDetailsInformation) = _iniConverterFactory.GetConverter(memberInfoWrapper.GetMemberType(), value, memberInfoWrapper); var iniContext = new IniContext(memberInfoWrapper, typeDetailsInformation, defaultIniValue, _iniParser, defaultConverter); var valueToSave = converter.FormatToWrite(value, iniContext); if (valueToSave?.Value == null) { return; } _iniParser.Write(valueToSave.Section, valueToSave.Key, valueToSave.Value); }
private static IDictionary CreateDictionary(IniContext iniContext) { var genericType = iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type; if (iniContext.TypeDetailsInformation.UnderlyingTypeInformation.TypeCode == TypeCode.Nullable) { genericType = typeof(Nullable <>).MakeGenericType(iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type); } var genericKeyType = iniContext.TypeDetailsInformation.UnderlyingKeyTypeInformation.Type; if (iniContext.TypeDetailsInformation.UnderlyingKeyTypeInformation.TypeCode == TypeCode.Nullable) { genericKeyType = typeof(Nullable <>).MakeGenericType(iniContext.TypeDetailsInformation.UnderlyingKeyTypeInformation.Type); } var dictionaryType = typeof(Dictionary <,>).MakeGenericType(genericKeyType, genericType); var returnedDictionary = (IDictionary)Activator.CreateInstance(dictionaryType); return(returnedDictionary); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { var enumerable = objectToFormat as IEnumerable; var index = 0; foreach (var item in enumerable) { if (item == null) { continue; } var dynamicIniOptionsAttribute = new IniOptionsAttribute() { Section = GenerateDynamicSection(iniContext, index) }; _iniWrapperWithCustomMemberInfo.SaveConfigurationWithCustomMemberInfo(item, dynamicIniOptionsAttribute); index++; } return(null); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { return(_iniWrapper.LoadConfiguration(destinationType)); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { throw new NotImplementedException(); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { iniContext.IniValue.Value = null; return(iniContext.IniValue); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { return(TimeSpan.Parse(readValue)); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { iniContext.IniValue.Value = objectToFormat.ToString(); return(iniContext.IniValue); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { return(System.Guid.Parse(readValue)); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContexte) { throw new TestCustomIniHandlerException("FormatToWrite"); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { objectToFormat = Activator.CreateInstance(iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type); return(_complexTypeIniConverter.FormatToWrite(objectToFormat, iniContext)); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { var ignoreAttribute = _memberInfoWrapper.GetAttribute <IniIgnoreAttribute>(); return(ignoreAttribute == null?_iniConverter.ParseReadValue(readValue, destinationType, iniContext) : null); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { _iniWrapper.SaveConfiguration(objectToFormat); return(null); }
private static string GenerateDynamicSection(IniContext iniContext, int i) { return($"{iniContext.TypeDetailsInformation.UnderlyingTypeInformation.Type.Name}_{i}"); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { throw new TestCustomIniHandlerException("ParseReadValue"); }
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext) { var ignoreAttribute = _memberInfoWrapper.GetAttribute <IniIgnoreAttribute>(); return(ignoreAttribute == null?_iniConverter.FormatToWrite(objectToFormat, iniContext) : null); }
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext) { return(readValue == TrueValue); }