检索 FormatException 参数
我正在使用这样的闭源第三方库:
object val = SomeClass.ExtractValue( someObject );
现在在更远的地方,第三方库尝试解析具有意外格式的 DateTime 值并抛出 FormatException。
在这种情况下,我想检索它尚未成功解析的字符串并尝试自己解析它。 像这样的事情:
object val;
try
{
val = SomeClass.ExtractValue( someObject );
}
catch( FormatException e )
{
string failed = e.GetParseArgument( );
val = DateTime.Parse( failed + " 2010" );
}
是的,简单地附加年份是毫无意义的,但你明白了。 第三方库不支持我需要的所有格式,但我也无法轻松地从“someObject”获取数据。 (是的,我可以尝试使用 Reflector 复制该库的功能,但我想避免这种情况。)
有什么方法可以做到这一点吗? 谢谢。
I'm using a closed-source third-party library like this:
object val = SomeClass.ExtractValue( someObject );
Now somewhere further down the road, the third-party library tries to parse a DateTime value that has an unexpected format and throws a FormatException.
In this case, I would like to retrieve the string that it hasn't succeeded to parse and try to parse it myself.
Something like this:
object val;
try
{
val = SomeClass.ExtractValue( someObject );
}
catch( FormatException e )
{
string failed = e.GetParseArgument( );
val = DateTime.Parse( failed + " 2010" );
}
Yes, simply appending the year is pretty pointless, but you get the idea.
The third-party library doesn't support all formats I need, but I can't easily get the data from "someObject" either.
(Yes, I could try to replicate what the library does using Reflector, but I'd like to avoid that.)
Is there any way to do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
someObject
是一个 IDataReader,因此您可以创建一个装饰器并将其传递给ExtractValue
。然后,您可以拦截日期字符串并在将其传递到库之前修改格式,例如,或者您可以记录从阅读器读取的所有值,然后您可以将失败的数据作为最后读取的项目获取并尝试自己解析它。
Since
someObject
is an IDataReader, you could create a decorator and pass that intoExtractValue
. You could then intercept the date string and modify the format before it gets passed to the library e.g.Alternatively you could record all values read from the reader, then you can get the failing data as the last read item and try to parse it yourself.