C# implicit conversion and conversion from Object

DeeRain

There is a class:

public class Date
{
    private DateTime _dateTime;

    public Date(DateTime dateTime)
    {
        _dateTime = dateTime;
    }

    public static implicit operator DateTime(Date d)
    {
        if (d == null)
            return default(DateTime);

        return d._dateTime;
    }

    public static implicit operator Date(DateTime dt)
    {
        return new Date(dt);
    }
}

So, this code works fine:

Date d = DateTime.Now;
DateTime dt=new Date(DateTime.Now);

But this code doesn't work and throws InvalidCastException "Specified cast is not valid.":

Date d = DateTime.Now;
var obj = (Object)d;
DateTime dt = (DateTime)obj;

For me, this is expected behavior, but is there any hack to make this particular code work? (Without cast obj to Date)

Servy

User defined implicit/explicit conversion operators are an entirely compile time construct. The compiler is going to say something alone the lines of, "Hey, this person is trying to stick a Foo somewhere that expects a Bar. Is a Foo a Bar? No, it's not. Hmm...Oh, I see that Foo has defined an implicit conversion to Bar, I'll go stick in a call to that static conversion method, so that at runtime all of the types match up."

The runtime has no knowledge about implicit/explicit conversions. By the time the program has finished being compiled, those are just regular static methods, like any other static method. Since the compiler only sees conversions from Date to object and from object to Date, it never sees a place where it needs to add the implicit conversion calls. By the time the runtime gets to it, it only sees that the Date isn't a DateTime.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# implicit conversion and conversion from Object

From Dev

C++ Implicit conversion from bool to string

From Dev

Implicit conversion from null

From Dev

c = a + b and implicit conversion

From Dev

C implicit conversion?

From Dev

c = a + b and implicit conversion

From Dev

Python: Implicit conversion of object to str?

From Dev

c++ User defined conversion - implicit conversion

From Dev

c++ User defined conversion - implicit conversion

From Dev

Implicit conversion during assignment in C?

From Dev

c# parameter implicit conversion

From Dev

c++: implicit conversion order

From Dev

Implicit integer type conversion in C

From Dev

strange implicit conversion on C++

From Dev

implicit conversion of vector from one type to another c++

From Dev

widening conversion not implicit when used with 'object' type?

From Dev

Implicit conversion from int to shared_ptr

From Dev

Implicit conversion from JToken in Json.NET

From Dev

Implicit conversion from char to char[] in F#?

From Dev

Implicit conversion from IEnumerable<int> to IEnumerable<dynamic>

From Dev

Implicit conversion failure from initializer list

From Dev

implicit conversion from scala Set to Java Set

From Dev

Cost of implicit conversion from java to scala collections

From Dev

Implicit conversion from nil to hash with refine bug

From Dev

Why is implicit conversion from int to Long not possible?

From Dev

Implicit conversion to Seq[T] from Array[T]

From Dev

There is no implicit reference conversion from ApplicationUser to IdentityUser

From Dev

Implicit conversion from Scala function to Java Function

From Dev

Implicit conversion from enumeration type with Xcode 6

Related Related

HotTag

Archive