現在、エラーに関するより良いレベルの詳細をユーザーに提供するために、次のようなものを作成する必要があります。
`` `c#
var e =新しいリスト
csvReader.Configuration.ReadingExceptionCallback =(ex、row)=>
{{
foreach(ex.Data.Valuesのvarエラー)
{{
var info = error.ToString()。Split(new [] {"\ n"、 "\ r \ n"}、StringSplitOptions.RemoveEmptyEntries);
var value = info [4] .Split( ':')[1] .Trim();
var field = info [3] .Split( ':')[1] .Trim();
e.Add(string.Format( "行{1}、フィールド{2}の値{0}を読み取れませんでした。"、
value、row.Row、field));
}
};
// eをWebページに返します(たとえば、JSONとして)
This is relatively ok. It gives information where exactly parsing error has occured. However, as you can see, digging this information is ugly since I have to parse string to get the value and the field name. It would be nice if this information could be exposed via properties so that I could write something like this:
``` c#
csvReader.Configuration.ReadingExceptionCallback = (ex, row) =>
{
foreach (var error in row.Errors)
{
e.Add(string.Format("Could not read value {0} in row {1}, field {2}.",
error.Value, error.Row, error.FieldName));
}
};
しかし、上記は大丈夫です...それは動作します。 さらに重要なのは、次のリクエストです。
また、構文解析が失敗した理由に関する情報を提供できると便利です。
フィールドが「赤」または「青」の値のみを受け入れるが、ユーザーが「黄色」と入力するとします。 これは次の方法で解決できます。
c#
// in the MyClassMap : CsvClassMap<MyClass> class
Map(x => x.Color).ErrorMessage("Accepted values are 'Red' and 'Blue'");
...
// in the ReadingExceptionCallback
e.Add(string.Format("Could not read value {0} in row {1}, field {2}. {3}",
error.Value, error.Row, error.FieldName, error.Message));
この構文は、開発者に多くの柔軟性を提供し、ユーザーに明確なフィードバックを提供します。
あなたの考えを聞かせてください。
これは興味深いアイデアです。 今のところこれを機能としてマークし、後で詳しく見ていきます。
この機能リクエストを2番目にしたいと思います。
私はここにいるので、簡単に感謝を述べたいと思います。 CsvHelperは非常に多くのレベルで優れています。
3.0では、ベースのCsvHelperException
オブジェクトにすべての値のプロパティが含まれるようになりました。 コールバックはException
代わりにCsvHelperException
を返します。 3.0ベータ版はNuGetにあります。
最も参考になるコメント
これは興味深いアイデアです。 今のところこれを機能としてマークし、後で詳しく見ていきます。