本文整理汇总了C#中FileHelpers.LineInfo类的典型用法代码示例。如果您正苦于以下问题:C# LineInfo类的具体用法?C# LineInfo怎么用?C# LineInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LineInfo类属于FileHelpers命名空间,在下文中一共展示了LineInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: StringToRecord
/// <summary>
/// Extract fields from record and assign values to the object
/// </summary>
/// <param name="record">Object to assign to</param>
/// <param name="line">Line of data</param>
/// <param name="values">Array of values extracted</param>
/// <returns>true if we processed the line and updated object</returns>
public bool StringToRecord(object record, LineInfo line, object[] values)
{
if (MustIgnoreLine(line.mLineStr))
return false;
for (int i = 0; i < RecordInfo.FieldCount; i++)
values[i] = RecordInfo.Fields[i].ExtractFieldValue(line);
try {
// Assign all values via dynamic method that
AssignHandler(record, values);
return true;
}
catch (InvalidCastException ex) {
// Occurs when a custom converter returns an invalid value for the field.
for (int i = 0; i < RecordInfo.FieldCount; i++) {
if (values[i] != null &&
!RecordInfo.Fields[i].FieldTypeInternal.IsInstanceOfType(values[i])) {
throw new ConvertException(null,
RecordInfo.Fields[i].FieldTypeInternal,
RecordInfo.Fields[i].FieldInfo.Name,
line.mReader.LineNumber,
-1,
Messages.Errors.WrongConverter
.FieldName(RecordInfo.Fields[i].FieldInfo.Name)
.ConverterReturnedType(values[i].GetType().Name)
.FieldType(RecordInfo.Fields[i].FieldInfo.FieldType.Name)
.Text
,
ex);
}
}
throw;
}
}
示例2: ExtractFieldString
protected override ExtractedInfo ExtractFieldString(LineInfo line)
{
if (mIsOptional && line.IsEOL() )
return ExtractedInfo.Empty;
if (mQuoteChar == '\0')
return BasicExtractString(line);
else
{
//TODO: UnComment and Fix
if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left)
{
//int pos = line.mCurrentPos;
line.TrimStart(mTrimChars);
// from2 = from.TrimStart(mTrimChars);
//res.CharsRemoved = line.mCurrentPos - pos;
}
string quotedStr = mQuoteChar.ToString();
if (line.StartsWith(quotedStr))
{
// ExtractedInfo res = null;
// res = new ExtractedInfo(line, line.mCurrentPos);
return StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead);
// if (mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead)
// {
//
// //res.ExtractedString = ei.ExtractedString;
// //res.CharsRemoved += ei.CharsRemoved;
// //res.ExtraLines = ei.ExtraLines;
// //res.NewRestOfLine = ei.NewRestOfLine;
// }
// else
// {
// return StringHelper.ExtractQuotedString(from2, mQuoteChar, out index);
// //res.CharsRemoved += index;
// }
// return res;
}
else
{
if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead)
return BasicExtractString(line);
else if (line.StartsWithTrim(quotedStr))
throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' has spaces before the QuotedChar at line "+ line.mReader.LineNumber.ToString() + ". Use the TrimAttribute to by pass this error. Field String: " + line.CurrentString);
else
throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' not begin with the QuotedChar at line "+ line.mReader.LineNumber.ToString() + ". You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: " + line.CurrentString);
}
}
}
示例3: ExtractedInfo
/// <summary>
/// Extract the rest of the line into my variable
/// </summary>
/// <param name="line"></param>
public ExtractedInfo(LineInfo line)
{
mLine = line;
ExtractedFrom = line.mCurrentPos;
ExtractedTo = line.mLineStr.Length - 1;
mCustomExtractedString = null;
}
示例4: ExtractFieldString
internal override ExtractedInfo ExtractFieldString(LineInfo line)
{
if (mIsOptional && line.IsEOL() )
return ExtractedInfo.Empty;
if (mQuoteChar == '\0')
return BasicExtractString(line);
else
{
if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left)
{
line.TrimStart(mTrimChars);
}
string quotedStr = mQuoteChar.ToString();
if (line.StartsWith(quotedStr))
{
return StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead);
}
else
{
if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead)
return BasicExtractString(line);
else if (line.StartsWithTrim(quotedStr))
throw new BadUsageException(string.Format("The field '{0}' has spaces before the QuotedChar at line {1}. Use the TrimAttribute to by pass this error. Field String: {2}", mFieldInfo.Name, line.mReader.LineNumber, line.CurrentString));
else
throw new BadUsageException(string.Format("The field '{0}' not begin with the QuotedChar at line {1}. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: {2}", mFieldInfo.Name, line.mReader.LineNumber, line.CurrentString));
}
}
}
示例5: ExtractFieldString
internal override ExtractedInfo ExtractFieldString(LineInfo line)
{
if (line.CurrentLength == 0)
{
if (mIsOptional)
return ExtractedInfo.Empty;
else
throw new BadUsageException("End Of Line found processing the field: " + mFieldInfo.Name + " at line "+ line.mReader.LineNumber.ToString() + ". (You need to mark it as [FieldOptional] if you want to avoid this exception)");
}
ExtractedInfo res;
if (line.CurrentLength < this.mFieldLength)
if (mFixedMode == FixedMode.AllowLessChars ||
mFixedMode == FixedMode.AllowVariableLength)
res = new ExtractedInfo(line);
else
throw new BadUsageException("The string '" + line.CurrentString + "' (length " + line.CurrentLength.ToString() + ") at line "+ line.mReader.LineNumber.ToString() + " has less chars than the defined for " + mFieldInfo.Name + " (" + mFieldLength.ToString() + "). You can use the [FixedLengthRecord(FixedMode.AllowLessChars)] to avoid this problem.");
else if (line.CurrentLength > mFieldLength &&
mIsArray == false &&
mIsLast &&
mFixedMode != FixedMode.AllowMoreChars &&
mFixedMode != FixedMode.AllowVariableLength)
throw new BadUsageException("The string '" + line.CurrentString + "' (length " + line.CurrentLength.ToString() + ") at line "+ line.mReader.LineNumber.ToString() + " has more chars than the defined for the last field " + mFieldInfo.Name + " (" + mFieldLength.ToString() + ").You can use the [FixedLengthRecord(FixedMode.AllowMoreChars)] to avoid this problem.");
else
res = new ExtractedInfo(line, line.mCurrentPos + mFieldLength);
return res;
}
示例6: ExtractedInfo
public ExtractedInfo(LineInfo line, int extractTo)
{
mLine = line;
ExtractedFrom = line.mCurrentPos;
ExtractedTo = extractTo - 1;
//CharsRemoved = ExtractedTo - ExtractedFrom + 1;
//ExtraLines = 0;
// NewRestOfLine = null;
}
示例7: ExtractFieldString
/// <summary>
/// Extract the field from the delimited file, removing separators and quotes
/// and any duplicate quotes within the record
/// </summary>
/// <param name="line">line containing record input</param>
/// <returns>Extract information</returns>
internal override ExtractedInfo ExtractFieldString(LineInfo line)
{
if (IsOptional && line.IsEOL())
return ExtractedInfo.Empty;
if (QuoteChar == '\0')
return BasicExtractString(line);
else {
if (TrimMode == TrimMode.Both ||
TrimMode == TrimMode.Left)
line.TrimStart(TrimChars);
string quotedStr = QuoteChar.ToString();
if (line.StartsWith(quotedStr)) {
var res = StringHelper.ExtractQuotedString(line,
QuoteChar,
QuoteMultiline == MultilineMode.AllowForBoth || QuoteMultiline == MultilineMode.AllowForRead);
if (TrimMode == TrimMode.Both ||
TrimMode == TrimMode.Right)
line.TrimStart(TrimChars);
if (!IsLast &&
!line.StartsWith(Separator) &&
!line.IsEOL()) {
throw new BadUsageException(line,
"The field " + this.FieldInfo.Name + " is quoted but the quoted char: " + quotedStr +
" not is just before the separator (You can use [FieldTrim] to avoid this error)");
}
return res;
}
else {
if (QuoteMode == QuoteMode.OptionalForBoth ||
QuoteMode == QuoteMode.OptionalForRead)
return BasicExtractString(line);
else if (line.StartsWithTrim(quotedStr)) {
throw new BadUsageException(
string.Format(
"The field '{0}' has spaces before the QuotedChar at line {1}. Use the TrimAttribute to by pass this error. Field String: {2}",
FieldInfo.Name,
line.mReader.LineNumber,
line.CurrentString));
}
else {
throw new BadUsageException(
string.Format(
"The field '{0}' does not begin with the QuotedChar at line {1}. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field. Field String: {2}",
FieldInfo.Name,
line.mReader.LineNumber,
line.CurrentString));
}
}
}
}
示例8: ExtractFieldValue
internal object ExtractFieldValue(LineInfo line)
{
//-> extract only what I need
if (InNewLine)
{
if (line.EmptyFromPos() == false)
throw new BadUsageException(line, "Text '" + line.CurrentString +
"' found before the new line of the field: " + FieldInfo.Name +
" (this is not allowed when you use [FieldInNewLine])");
line.ReLoad(line.mReader.ReadNextLine());
if (line.mLineStr == null)
throw new BadUsageException(line, "End of stream found parsing the field " + FieldInfo.Name +
". Please check the class record.");
}
if (IsArray == false)
{
ExtractedInfo info = ExtractFieldString(line);
if (info.mCustomExtractedString == null)
line.mCurrentPos = info.ExtractedTo + 1;
line.mCurrentPos += CharsToDiscard; //total;
return AssignFromString(info, line);
}
else
{
if (ArrayMinLength <= 0)
ArrayMinLength = 0;
int i = 0;
var res = new ArrayList(Math.Max(ArrayMinLength, 10));
while (line.mCurrentPos - CharsToDiscard < line.mLine.Length && i < ArrayMaxLength)
{
ExtractedInfo info = ExtractFieldString(line);
if (info.mCustomExtractedString == null)
line.mCurrentPos = info.ExtractedTo + 1;
line.mCurrentPos += CharsToDiscard;
res.Add(AssignFromString(info, line));
i++;
}
if (res.Count < ArrayMinLength)
throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), FieldInfo.Name, res.Count, ArrayMinLength));
else if (IsLast && line.IsEOL() == false)
throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, FieldInfo.Name, ArrayMaxLength));
return res.ToArray(ArrayType);
}
}
示例9: AssignFromString
internal object AssignFromString(ExtractedInfo fieldString, LineInfo line)
{
object val;
var extractedString = fieldString.ExtractedString();
var trimmedBoth = false;
switch (TrimMode)
{
case TrimMode.None:
break;
case TrimMode.Both:
extractedString = extractedString.Trim();
trimmedBoth = true;
//fieldString.TrimBoth(TrimChars);
break;
case TrimMode.Left:
extractedString = extractedString.TrimStart();
//fieldString.TrimStart(TrimChars);
break;
case TrimMode.Right:
extractedString = extractedString.TrimEnd();
//fieldString.TrimEnd(TrimChars);
break;
}
try
{
if (ConvertProvider == null)
{
if (IsStringField)
val = extractedString;
else
{
// Trim it to use Convert.ChangeType
if (trimmedBoth == false)
extractedString = extractedString.Trim();
if (extractedString.Length == 0)
{
// Empty stand for null
val = GetNullValue(line);
}
else
{
val = Convert.ChangeType(extractedString, FieldTypeInternal, null);
}
}
}
else
{
var trimmedString = extractedString;
if (trimmedBoth == false)
{
trimmedString = extractedString.Trim();
}
if (ConvertProvider.CustomNullHandling == false &&
trimmedString.Length == 0)
{
val = GetNullValue(line);
}
else
{
string from = extractedString;
val = ConvertProvider.StringToField(from);
if (val == null)
val = GetNullValue(line);
}
}
return val;
}
catch (ConvertException ex)
{
ex.FieldName = FieldInfo.Name;
ex.LineNumber = line.mReader.LineNumber;
ex.ColumnNumber = fieldString.ExtractedFrom + 1;
throw;
}
catch (BadUsageException)
{
throw;
}
catch (Exception ex)
{
if (ConvertProvider == null || ConvertProvider.GetType().Assembly == typeof(FieldBase).Assembly)
throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex);
else
throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + ConvertProvider.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex);
}
}
示例10: NullValueNotFoundException
/// <summary>Creates an instance of an NullValueNotFoundException.</summary>
/// <param name="message">The exception Message</param>
/// <param name="line">Line to display in message</param>
internal NullValueNotFoundException(LineInfo line, string message)
: base(line, message)
{
}
示例11: StringToRecord
public object StringToRecord(LineInfo line, object[] values)
{
if (MustIgnoreLine(line.mLineStr))
return null;
for (int i = 0; i < RecordInfo.FieldCount; i++)
{
values[i] = RecordInfo.Fields[i].ExtractFieldValue(line);
}
try
{
// Asign all values via dinamic method that creates an object and assign values
return CreateHandler(values);
}
catch (InvalidCastException ex)
{
// Occurrs when the a custom converter returns an invalid value for the field.
for (int i = 0; i < RecordInfo.FieldCount; i++)
{
if (values[i] != null && !RecordInfo.Fields[i].FieldTypeInternal.IsInstanceOfType(values[i]))
throw new ConvertException(null,
RecordInfo.Fields[i].FieldTypeInternal,
RecordInfo.Fields[i].FieldInfo.Name,
line.mReader.LineNumber,
-1,
Messages.Errors.WrongConverter
.FieldName(RecordInfo.Fields[i].FieldInfo.Name)
.ConverterReturnedType(values[i].GetType().Name)
.FieldType(RecordInfo.Fields[i].FieldInfo.FieldType.Name)
.Text
,
ex);
}
return null;
}
}
示例12: BasicExtractString
private ExtractedInfo BasicExtractString(LineInfo line)
{
ExtractedInfo res;
if (mIsLast)
res = new ExtractedInfo(line);
else
{
int sepPos;
sepPos = line.IndexOf(mSeparator);
if (sepPos == -1)
{
if (this.mNextIsOptional == false)
{
string msg = null;
if (mIsFirst && line.EmptyFromPos())
msg = "The line " + line.mReader.LineNumber.ToString() + " is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.";
else
msg = "The delimiter '" + this.mSeparator + "' can´t be found after the field '" + this.mFieldInfo.Name + "' at line " + line.mReader.LineNumber.ToString() + " (the record has less fields, the delimiter is wrong or the next field must be marked as optional).";
throw new FileHelpersException(msg);
}
else
sepPos = line.mLine.Length - 1;
}
res = new ExtractedInfo(line, sepPos);
}
return res;
}
示例13: BasicExtractString
private ExtractedInfo BasicExtractString(LineInfo line)
{
if (IsLast && ! IsArray)
return new ExtractedInfo(line);
else
{
int sepPos;
sepPos = line.IndexOf(mSeparator);
if (sepPos == -1)
{
if (IsLast && IsArray)
return new ExtractedInfo(line);
if (NextIsOptional == false)
{
string msg;
if (IsFirst && line.EmptyFromPos())
msg = string.Format("The line {0} is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.", line.mReader.LineNumber);
else
msg = string.Format("Delimiter '{0}' not found after field '{1}' (the record has less fields, the delimiter is wrong or the next field must be marked as optional).", mSeparator, this.FieldInfo.Name, line.mReader.LineNumber);
throw new FileHelpersException(line.mReader.LineNumber, line.mCurrentPos, msg);
}
else
sepPos = line.mLine.Length;
}
return new ExtractedInfo(line, sepPos);
}
}
示例14: GetNullValue
/// <summary>
/// Convert a null value into a representation,
/// allows for a null value override
/// </summary>
/// <param name="line">input line to read, used for error messages</param>
/// <returns>Null value for object</returns>
private object GetNullValue(LineInfo line)
{
if (NullValue == null) {
if (FieldTypeInternal.IsValueType) {
if (IsNullableType)
return null;
string msg = "Not value found for the value type field: '" + FieldInfo.Name + "' Class: '" +
FieldInfo.DeclaringType.Name + "'. " + Environment.NewLine
+
"You must use the [FieldNullValue] attribute because this is a value type and can't be null or use a Nullable Type instead of the current type.";
throw new NullValueNotFoundException(line, msg);
}
else
return null;
}
else
return NullValue;
}
示例15: ExtractFieldString
protected abstract ExtractedInfo ExtractFieldString(LineInfo line);