gpt4 book ai didi

c# - 如何使用必需的非空列设置 filehelpers

转载 作者:行者123 更新时间:2023-11-30 13:33:01 26 4
gpt4 key购买 nike

我一直在查看 filehelpers 文档,但似乎没有任何内容可以处理列中的空值。我需要能够在所有列上设置“非空”字符串属性。

谁能指出我正确的方向?

最佳答案

您可以在 AfterReadRecord 事件中执行任何您想要的验证。如果您希望在出现错误时继续处理文件的其余部分,您还需要将 ErrorMode 设置为 SaveAndContinue。请参阅下面的工作示例。

[DelimitedRecord("|")]
public class MyClass
{
public string Field1;
public string Field2;
public string Field3;
}

class Program
{
static void Main(string[] args)
{
var engine = new FileHelperEngine<MyClass>();
engine.AfterReadRecord += new FileHelpers.Events.AfterReadHandler<MyClass>(engine_AfterReadRecord);
engine.ErrorMode = ErrorMode.SaveAndContinue;
// import a record with an invalid Email
MyClass[] validRecords = engine.ReadString("Hello||World");
ErrorInfo[] errors = engine.ErrorManager.Errors;
Assert.AreEqual(1, engine.TotalRecords); // 1 record was processed
Assert.AreEqual(0, validRecords.Length); // 0 records were valid
Assert.AreEqual(1, engine.ErrorManager.ErrorCount); // 1 error was found
Assert.That(errors[0].ExceptionInfo.Message == "Field2 is invalid");
}

static void engine_AfterReadRecord(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<MyClass> e)
{
if (String.IsNullOrWhiteSpace(e.Record.Field1))
throw new Exception("Field1 is invalid");
if (String.IsNullOrWhiteSpace(e.Record.Field2))
throw new Exception("Field2 is invalid");
if (String.IsNullOrWhiteSpace(e.Record.Field3))
throw new Exception("Field3 is invalid");
}
}

关于c# - 如何使用必需的非空列设置 filehelpers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10206514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com