-
Notifications
You must be signed in to change notification settings - Fork 0
Custom Converter
Szpi edited this page Sep 30, 2018
·
4 revisions
If you need special behaviour of parsing / reading property from configuration class you can implement IIniConverter and use IniConverter Attribute. Library will call ParseReadValue when reading from ini file and FormatToWrite when saving.
Example:
Configuration class and converter implementation:
public class BoolBinaryConverterConfiguration
{
[IniConverter(typeof(BoolBinaryConverter))]
public bool TestBool { get; set; }
}
public class BoolBinaryConverter : IIniConverter
{
private const string TrueValue = "1";
private const string FalseValue = "0";
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext)
{
return readValue == TrueValue;
}
public IniValue FormatToWrite(object objectToFormat, IniContext iniContext)
{
var castedValue = (bool)objectToFormat;
iniContext.IniValue.Value = castedValue ? TrueValue : FalseValue;
return iniContext.IniValue;
}
}
You can pass CustomConverter parameters via IniConverter's property ConverterParameters. See example below.
public class IniConverterWithConstructorParameters
{
[IniConverter(typeof(CustomIniConverterWithConstructor), new object[] { "Argument", 10 })]
public TestEnum TestEnum { get; set; }
}
Method should return IniValue that will be written to ini file. If IniValue.Value is null library doesn't invoke Write method on IIniParser.
Method should return object that will be set as value to processed type. Type of returning object should match destinationType.
- EnumStringConverter
- BoolBinaryConverter
- IMemberInfoWrapper From this interface you can get Attributes, Type.
- TypeDetailsInformation Detailed information about type that is being processed. UnderlyingTypeInformation is set only when type is nullable, IEnurable or IDictionary. UnderlyingKeyTypeInformation is only set for IDictionary and contains information about Key in this structure.
- IniValue Default ini value for Section and Key described in How does it work?
- IIniParser Interface to read or save from ini file.
- IIniConverter Default converter for type that is being processed.