How to make class in C#, that can be cast to DateTime?
Asked Answered
S

4

15

How can I make class, that can be cast to DateTime. But I need to cast my class, when it packed. For example:

object date1 = new MyDateTime();
DateTime date2 = (DateTime)date1;

I need directly this working example.

I know how to do it, but my way will work without packing. I'm not sure is there way to do it.

Please, help.

PS. I need cast directly object to DateTime. So, MyDateTime have to be packed before. Explicit works well, but it doesn't help if you have packed object. And it have to cast just using ordinary casting like

(DateTime) (object) MyDateTime
Squadron answered 12/4, 2012 at 10:16 Comment(1)
As described, this is impossible. You can't use Convert.ToDateTime? Why does it need to be an object variable?Polymorphonuclear
I
31

What you appear to be after is inheritance, being able to "store" a derived class instance in a variable of the base type like so:

Stream s = new FileStream();

The fact that it is a FileStream under the hood is not lost just because you are pointing to it with the Stream goggles on.

DateTime is a struct, and struct inheritance is not supported - so this is not possible.

An alternative is the explicit keyword for user-defined conversions (syntactically looking like casts). This allows you to at least interchange between your class and DateTime with more sugar.

This could look like:

class MyDateTime
{
    private DateTime _inner;

    public static explicit operator DateTime(MyDateTime mdt)
    {
        return mdt._inner;
    }
}

You can do the same with the counterpart implicit keyword:

public static implicit operator DateTime(MyDateTime mdt)
{
    return mdt._inner;
}

That then lets you do the "casting" implicitly:

DateTime date = new MyDateTime();

Another alternative is to wrap DateTime with your own adapter class that internally uses a DateTime and then inherit from this class to create MyDateTime. Then instead of using DateTime in your code base, you use this adapter class.

I've seen similar things with SmartDateTime style classes where the DateTime has a better understanding of nulls and if it was set.

Illomened answered 12/4, 2012 at 10:18 Comment(4)
@EvgenyEkim What you are looking for is inheritance then, if you want to store a derived type in a reference of a base type. This is not possible with DateTime as it is a struct.Illomened
Yes, you're right. But I don't know is there way to do it without inheritance.Squadron
@EvgenyEkim You can store a DateTime in your own class, but you cannot store your own class in a DateTime. I have demonstrated the former.Illomened
Sorry. I didn't read all answer. Thank you very much for answer.Squadron
I
11

You could use

class MyDateTime
{
    public static explicit operator DateTime(MyDateTime dt)
    {
        return new DateTime(); // Convert dt here            
    }
}
Ilo answered 12/4, 2012 at 10:19 Comment(0)
F
2

Instead of a direct cast, I would create an extension method that performs the required steps to complete the conversion. Some sort of conversion is necessary, because DateTime can't be inherited from, so a direct cast will be impossible.

public static class DateTimeExtensions
{
    public static MyDateTime ToMyDateTime(this DateTime dt)
    {
        //conversion logic
        return myDataTime;
    }
    public static DateTime ToDateTime(this MyDateTime mdt)
    {
        //conversion logic
        return dateTime;
    }
}

In this way, you would use the code like so:

DateTime dt = mdt.ToDateTime();

or

MyDateTime mdt = dt.ToMyDateTime();
Fafnir answered 8/4, 2015 at 2:24 Comment(0)
P
0

You'd need to write an explicit cast operator on your MyDateTime class that calls into one of the constructors on Datetime. Here's an example.

Phototype answered 12/4, 2012 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.