I have a Castle ActiveRecord class with a DateTime property. I am importing data from a text file, and would love to be able to do something like this:
string date_started = "09/25/2009";
MyClass myclass = new MyClass;
myclass.date_started = date_started;
On the final assignment, behind the scenes, it would ideally check the type of date_started
, and if it is DateTime, do the assignment, otherwise do Convert.ToDateTime(date_started)
.
I can't override accessors [*], and implicit operators only work when converting to or from the containing class. I tried extending DateTime with an implicit operator conversion, but found out it was sealed. Now I am a very unhappy dynamic programmer stuck in a statically-typed world.
I could of course do the check "manually," but I am instantiating many different objects with many properties, and was hoping to be able to loop over them (using reflection), without having to give specific properties special treatment. I could define my own custom accessors, but that again requires special treatment at assignment, since they need to be used like methods (setX(val)
) and not properties (X = val
).
Can C# (2.0) or Castle ActiveRecord offer me any clean way to get a String -> DateTime conversion in the background?