views:

637

answers:

3

For some reason I need to cast/convert a DateTime into one of many custom objects

This proves to be very difficult to do in a nice generic fashion.

I am thinking of implementing an extension method on object or perhaps extending DateTimeConverter.

But then what would be the generic way to handle this, I have an object and a destination type and at the moment I am using System.ConvertTo(..) but this is clearly limited because it only supports converting to .NET types and cant be extended.

Any ideas?

A: 

I can quite understand the intitial reaction to this: why in the hell do you want to do that..

Its tempting for me to say "trust me" but for anyone with 2 minutes of patience let me explain, maybe I have this back to front:

I have a db and a LINQ to SQL DataContext

My datacontext objects supply a more complex object model.

LINQ to SQL only supports mapping to certain types and does not support Many to many relationships and leaves me having to hard code the mapping.

I use reflection to populate "real world" object Properties from DataContext objects.

The real world object has more complex types such a TimeStamp (DateTime with extended functionality). So during this stage I have to convert a L2SQL generated System.DateTime object to my TimeStamp object.

Still following??

Ok so the problem is in 95% of cases the method correctly converts L2SQL types to the correct destination type e.g numeric(3,0) => (LINQ) System.Decimal => Int (real world object).

But I have properties that are wrappers for DateTime with extra functionality, clearly the raw value is a DateTime so when I save to the database a DateTime is sufficient. But when it comes back from the database I cannot get that sql datetime into my TimeStamp object...

I am now resigned to writing code along the lines you suggested:

if LINQ type == DateTime and destination == TimeStamp then .....

but I thought there must be a nice way to convert DateTime to some other type..

Maybe not.

Tom Deloford
A: 

You could add a constructor for your TimeStamp that takes DateTime as a parameter.

public class TimeStamp
{
   public TimeStamp(DateTime dateTime, ...)
   {
    // Your code here to convert from dateTime to TimeStamp
   }
}

Then later ...

 DestType Convert<DestType>( Object linqResult )   
 {
  DestType result;
  if( linqResult is DateTime && DestType is TimeStamp )
  {
   DateTime dbTime = linqResult as DateTime;
   result = new TimeStamp( dbTime );
  }
  ...
  return result;
 }

You could probably even extend this with all kinds of reflection and constructors etc. But if you want to base one object off of another (like a time stamp from a date time) a constructor seems natural.

Hope this helps, TJB

TJB
A: 

TJB thats exactly what I settled on and its working nicely.

Thanks

Tom Deloford
Good to hear, you should probably put posts like this as comments on my answer, technically what you posted is an answer = )That's just the unique form of this site though.Good Luck Coding!
TJB