本文整理汇总了C#中System.IO.StringReader.TryRead方法的典型用法代码示例。如果您正苦于以下问题:C# StringReader.TryRead方法的具体用法?C# StringReader.TryRead怎么用?C# StringReader.TryRead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.StringReader
的用法示例。
在下文中一共展示了StringReader.TryRead方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parse
/// <summary>
/// Parses a single configuration line.
/// </summary>
/// <param name="lineIndex">The index of the line.</param>
/// <param name="line">The contents of the line.</param>
/// <param name="currentSectionName">The name of the current section.</param>
private void Parse(int lineIndex, string line, ref string currentSectionName)
{
char c;
var reader = new StringReader(line);
reader.SkipWhitespace();
// First character
if (!reader.TryRead(out c))
return;
if (c == '[') {
// Read section name
var sectionNameBuilder = new StringBuilder();
while (reader.TryRead(out c) && c != ']') {
sectionNameBuilder.Append(c);
}
if (c != ']')
throw new Exception("Invalid section name");
// Set and add section
currentSectionName = sectionNameBuilder.ToString().Trim();
Section section;
if (!_sections.TryGetValue(currentSectionName, out section))
_sections.Add(currentSectionName, section = new Section(currentSectionName));
section.Lines.Add(lineIndex);
} else if (c != ';' && c != '#') {
// Read property name and value
var propertyNameBuilder = new StringBuilder();
propertyNameBuilder.Append(c);
while (reader.TryRead(out c) && c != '=' && c != ':') {
propertyNameBuilder.Append(c);
}
if (c != '=' && c != ':')
throw new Exception("Expected = or : after property name");
string propertyName = propertyNameBuilder.ToString().Trim();
string value = ReadPropertyValue(reader);
// Set or add property
Property property = GetOrAddProperty(currentSectionName, propertyName);
property.Value = value;
property.Lines.Add(lineIndex);
}
}