1

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.

Andreas
  • 23
  • 2
  • Do you have control over the input to the 3rd party library? IE, could you parse the dateTime in SomeObject, and re-save it in a format you know the 3rd party can accept before passing it in? – Andrew M Dec 09 '10 at 18:16
  • Nope. Actually, someObject is an IDataReader object. – Andreas Dec 09 '10 at 18:17

1 Answers1

0

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.

Lee
  • 142,018
  • 20
  • 234
  • 287
  • Thanks, it's not quite as simple a solution as I'd hoped, but I'll give it a shot. – Andreas Dec 10 '10 at 16:23
  • @Andreas - Since IDataReader is quite a large interface it may be easier to use a dynamic proxy like linfu and intercept the required method instead of creating your own custom decorator class. – Lee Dec 10 '10 at 18:53