/// <summary> /// Initalizes a <see cref="FixedLengthFieldString"/> object with a definition of a set of fixed-length fields /// and a string. The string will be parsed and values stored in the string will be assigned to the fields. /// </summary> /// <param name="fields">Defintion of fields in the string.</param> /// <param name="s">The string to be parsed in to the fields.</param> public FixedLengthFieldString(FixedLengthFieldCollection fields, string s) { ParameterGuard.NullCheck(s, nameof(s)); ParameterGuard.NullCheck(fields, nameof(fields)); Fields = fields; Parse(s); }
/// <summary> /// Set the value of a key. /// </summary> /// <param name="section">Section name.</param> /// <param name="key">Parameter key.</param> /// <param name="value">Value to be set.</param> public void WriteValue <T>(string section, string key, T value) where T : IConvertible { ParameterGuard.NullOrEmptyStringCheck(section, nameof(section)); ParameterGuard.NullOrEmptyStringCheck(key, nameof(key)); WritePrivateProfileString(section, key, Convert.ToString(value), Path); }
public static void Log(ILogDefinition def, params object[] args) { ILogger logger = _logger; ParameterGuard.NullCheck(logger, nameof(_logger)); logger.Log(def, args); }
static void Main(string[] args) { List <string> list = new List <string>(); list.Add("test"); ParameterGuard.ThrowIfNullOrEmpty(list, "list"); ParameterGuard.ThrowIfNullOrEmpty("string", "str"); }
/// <summary> /// Read the value of a key. /// </summary> /// <param name="section">Section name.</param> /// <param name="key">Parameter key.</param> /// <param name="defaultValue">Default value to be returned if the key does not exist.</param> /// <returns>The value if the key exists; otherwise, the default value.</returns> public T ReadValue <T>(string section, string key, T defaultValue = default(T)) where T : IConvertible { ParameterGuard.NullOrEmptyStringCheck(section, nameof(section)); ParameterGuard.NullOrEmptyStringCheck(key, nameof(key)); StringBuilder value = new StringBuilder(SingleValueSize); GetPrivateProfileString(section, key, Convert.ToString(defaultValue), value, 255, Path); return((T)Convert.ChangeType(value, typeof(T))); }
/// <summary> /// Instantiate an INI file object with the path of the INI file. /// </summary> /// <param name="path">Path of the INI file.</param> public IniFile(string path) { ParameterGuard.NullOrEmptyStringCheck(path, nameof(path), "Path of ini file must not be null or empty."); if (!File.Exists(path)) { throw new FileNotFoundException("Ini file does not exist.", path); } Path = path; }
public void ParameterGuard_DefaultValueCheck_CheckDefaultValueWithoutGivenMessage_ShouldThrowArgumentExceptionWithGivenParamNameAndMessage() { try { ParameterGuard.DefaultValueCheck(default(int), ParameterName); } catch (ArgumentException ex) { ValidateExceptionParameterName(ex, ParameterName); // If value of ex.ParamName is expected, rethrow the ArgumentException to // make the test pass. throw; } }
public void ParameterGuard_NullCheck_CheckNullValueWithoutGivenMessage_ShouldThrowArgumentNullExceptionWithGivenParamNameAndMessage() { try { ParameterGuard.NullCheck <object>(null, ParameterName); } catch (ArgumentNullException ex) { ValidateExceptionParameterName(ex, ParameterName); // If value of ex.ParamName is expected, rethrow the ArgumentNullException to // make the test pass. throw; } }
public void ParameterGuard_NullOrEmptyStringCheck_CheckEmptyStringWithoutGivenMessage_ShouldThrowArgumentException() { try { ParameterGuard.NullOrEmptyStringCheck(String.Empty, ParameterName); } catch (ArgumentException ex) { ValidateExceptionParameterName(ex, ParameterName); // If value of ex.ParamName is expected, rethrow the ArgumentException to // make the test pass. throw; } }
public void ParameterGuard_NullOrEmptyStringCheck_CheckEmptyStringWithGivenMessage_ShouldThrowArgumentException() { try { ParameterGuard.NullOrEmptyStringCheck(String.Empty, ParameterName, CustomExceptionMessage); } catch (ArgumentException ex) { ValidateExceptionParameterName(ex, ParameterName); ValidateExceptionMessage(ex, GetExpectedExceptionMessage(ex, CustomExceptionMessage)); // If values of both ex.Message are ex.ParamName are expected, rethrow the ArgumentException to // make the test pass. throw; } }
public static string Pad(this string s, int length, PaddingCharPosition position, char paddingChar) { ParameterGuard.NullCheck(s, nameof(s)); switch (position) { case PaddingCharPosition.Left: return(s.PadLeft(length, paddingChar)); case PaddingCharPosition.Right: return(s.PadRight(length, paddingChar)); default: throw new NotSupportedException($"The value of {nameof(position)} ({position}) is not supported."); } }
public static string TrimPaddingChar(this string s, PaddingCharPosition position, char paddingChar) { ParameterGuard.NullCheck(s, nameof(s)); switch (position) { case PaddingCharPosition.Left: return(s?.TrimStart(paddingChar)); case PaddingCharPosition.Right: return(s?.TrimEnd(paddingChar)); default: throw new NotSupportedException($"The value of {nameof(position)} ({position}) is not supported."); } }
/// <summary> /// Read all parameter keys and values defined under a given section in the INI file. /// </summary> /// <param name="section">Section name.</param> /// <returns>A set of parameter keys and values.</returns> public IDictionary <string, T> ReadAllKeysAndValues <T>(string section) where T : IConvertible { ParameterGuard.NullOrEmptyStringCheck(section, nameof(section)); byte[] buffer = new byte[SectionBufferSize]; GetPrivateProfileSection(section, buffer, SectionBufferSize, Path); string[] parameters = Encoding.ASCII.GetString(buffer) .Trim('\0') .Split(new[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); var paramToReturn = parameters.Select(p => new { Str = p, EqualSignIndex = p.IndexOf('=') }); return(paramToReturn .Where(p => p.EqualSignIndex >= 0) .Select(p => new { Key = p.Str.Substring(0, p.EqualSignIndex), Value = (T)Convert.ChangeType(p.Str.Substring(p.EqualSignIndex + 1), typeof(T)) }) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); }
public void ParameterGuard_DefaultValueCheck_CheckNonDefaultValueWithGivenMessage_ShouldNotThrowException() { int i = 100; ParameterGuard.DefaultValueCheck(i, ParameterName, CustomExceptionMessage); }
public void ParameterGuard_EmptyStringCheck_CheckNonEmptyAndNonNullValueWithGivenMessage_ShouldNotThrowException() { ParameterGuard.EmptyStringCheck(NonEmptyString, ParameterName, CustomExceptionMessage); }
public void ParameterGuard_EmptyStringCheck_CheckNullValueWithoutGivenMessage_ShouldNotThrowException() { ParameterGuard.EmptyStringCheck(null, ParameterName); }
public void ParameterGuard_NullOrEmptyStringCheck_CheckNonEmptyAndNonNullValueWithoutGivenMessage_ShouldNotThrowException() { ParameterGuard.NullOrEmptyStringCheck(NonEmptyString, ParameterName); }
public void ParameterGuard_NullCheck_CheckNonNullValueWithoutGivenMessage_ShouldNotThrowException() { object nonNullObject = new object(); ParameterGuard.NullCheck(nonNullObject, ParameterName); }