检索 FormatException 参数

发布于 2024-10-07 11:46:01 字数 569 浏览 2 评论 0原文

我正在使用这样的闭源第三方库:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2024-10-14 11:46:01

由于 someObject 是一个 IDataReader,因此您可以创建一个装饰器并将其传递给 ExtractValue。然后,您可以拦截日期字符串并在将其传递到库之前修改格式,例如

public class DateFormattingDataReader : IDataReader
{
    private readonly IDataReader inner;

    public DateFormattingDataReader(IDataReader inner)
    {
        this.inner = inner;
    }

    public string GetString(int index)
    {
        string s = this.inner.GetString(index);
        if(index == problematicColumnIndex)
        {
            //try to parse string and then format it for the library
        }
        else return s;
    }
}

,或者您可以记录从阅读器读取的所有值,然后您可以将失败的数据作为最后读取的项目获取并尝试自己解析它。

Since someObject is an IDataReader, you could create a decorator and pass that into ExtractValue. You could then intercept the date string and modify the format before it gets passed to the library e.g.

public class DateFormattingDataReader : IDataReader
{
    private readonly IDataReader inner;

    public DateFormattingDataReader(IDataReader inner)
    {
        this.inner = inner;
    }

    public string GetString(int index)
    {
        string s = this.inner.GetString(index);
        if(index == problematicColumnIndex)
        {
            //try to parse string and then format it for the library
        }
        else return s;
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文