本文整理汇总了C#中ParseContext.Handle方法的典型用法代码示例。如果您正苦于以下问题:C# ParseContext.Handle方法的具体用法?C# ParseContext.Handle怎么用?C# ParseContext.Handle使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ParseContext
的用法示例。
在下文中一共展示了ParseContext.Handle方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadFrom
public IEnumerable<Record> ReadFrom(Stream stream, string fieldSeparator = ",", string recordSeparator = "\r\n", bool supportQuotedFields = true, bool hasHeaderRow = false, IDictionary<string, Func<string, object>> namedFieldConverters = null, IDictionary<int, Func<string, object>> indexedFieldConverters = null)
{
if (stream == null)
{
throw new ArgumentException(ErrorNoStream);
}
if (fieldSeparator == null)
{
fieldSeparator = "";
}
if (recordSeparator == null)
{
recordSeparator = "";
}
if (fieldSeparator == recordSeparator)
{
throw new ParseException(ErrorFieldSeparatorAndRecordSeparator);
}
if (namedFieldConverters != null && indexedFieldConverters != null)
{
throw new ArgumentException(ErrorBothNamedAndIndexedFieldConverters);
}
if (namedFieldConverters != null && !hasHeaderRow)
{
throw new ArgumentException(ErrorNamedFieldConverters);
}
var bufferedStream = new BufferedStream(stream, 131072);
var fieldValues = new List<object>();
var parseContext = new ParseContext
{
HeaderRowFieldIndexes = new Dictionary<string, int>(),
HasHeaderRow = hasHeaderRow,
RecordSeparator = recordSeparator,
FieldSeparator = fieldSeparator,
ReadingHeaderRow = hasHeaderRow,
Handle = StartReadField,
RecordNumber = 1,
SupportQuotedFields = supportQuotedFields,
Capture = new StringBuilder(),
NamedFieldConverters = namedFieldConverters,
IndexedFieldConverters = indexedFieldConverters,
StringCache = new Dictionary<string, string>()
};
var specialTokens = new List<string>();
if (!String.IsNullOrEmpty(fieldSeparator))
{
specialTokens.Add(fieldSeparator);
}
if (!String.IsNullOrEmpty(recordSeparator))
{
specialTokens.Add(recordSeparator);
}
if (supportQuotedFields)
{
specialTokens.Add("\"");
}
var specialChars = specialTokens.SelectMany(x => x).Distinct().ToArray();
foreach (var token in _streamTokenizer.Tokenize(bufferedStream, specialChars))
{
var yieldIt = parseContext.Handle(token, fieldValues, parseContext);
if (yieldIt)
{
yield return new Record(fieldValues, parseContext.HeaderRowFieldIndexes);
fieldValues = new List<object>(parseContext.HasHeaderRow
? parseContext.HeaderRowFieldIndexes.Count
: fieldValues.Count);
parseContext.Capture.Length = 0;
}
}
if (parseContext.Handle == ReadQuotedField)
{
throw new ParseException(string.Format(ErrorQuotedFieldDoesNotHaveACloseQuoteOnLineField, parseContext.RecordNumber, parseContext.FieldNumber));
}
if (parseContext.ReadingQuotedField &&
(parseContext.Handle == ContinueCollectFieldSeparator || parseContext.Handle == ContinueCollectRecordSeparator))
{
ThrowUnescapedQuoteException(parseContext);
}
if (parseContext.Capture.Length > 0 ||
parseContext.FieldNumber > 0 &&
(parseContext.Handle == StartReadField || parseContext.Handle == BranchReadEscapedQuoteOrStartCollectingSeparator))
{
if (parseContext.Handle == StartReadField)
{
parseContext.IncrementFieldNumber();
}
AddFieldValue(fieldValues, parseContext);
}
parseContext.StringCache = null;
if (fieldValues.Count > 0)
{
yield return new Record(fieldValues, parseContext.HeaderRowFieldIndexes);
}
//.........这里部分代码省略.........
示例2: StartCollectingFieldSeparator
private bool StartCollectingFieldSeparator(Token token, List<object> fieldValues, ParseContext parseContext)
{
parseContext.SeparatorLength = 0;
var fieldSeparator = parseContext.FieldSeparator;
var recordSeparator = parseContext.RecordSeparator;
if (recordSeparator.Length > 0 && fieldSeparator[0].Equals(recordSeparator[0]) && recordSeparator.Length > fieldSeparator.Length)
{
parseContext.Handle = ContinueCollectRecordSeparator;
}
else
{
parseContext.Handle = ContinueCollectFieldSeparator;
}
return parseContext.Handle(token, fieldValues, parseContext);
}