I have a "JulianDate" struct that I've written in C#, and it has a custom explicit operator to DateTime from the .NET Library. I've used this explicit operator several times in my C# code and it works with no issues.
I now want to use the same explicit operator in my C++/CLI code, but I am unable to figure out how.
I've tried:
DateTime^ dt = (DateTime^)jdate;
(compiles, but I get an InvalidCastException)DateTime^ dt = safe_cast<DateTime^>(jdate);
(I get a compiler error)DateTime^ dt = DateTime(*jdate);
(compiles, but dt has wrong data: 1/1/0001 12:00AM)DateTime^ dt = dynamic_cast<DateTime^>(jdate);
(compiles, but returns null)For the safe cast, I'm getting the following error:
`Error 4 error C2682: cannot use 'safe_cast' to convert from 'Solution::Common::JulianDate ^' to 'System::DateTime ^' C:\Users\9a3eedi\Documents\Solution\Wrapper\Wrapper.cpp 75 Wrapper
What is the proper method of performing an explicit cast? Or is the reason why it's not working is due to me working with structs and not with classes? Or perhaps C++/CLI does not support C# explicit operators?
DateTime^ dt = (DateTime^)jdate;
Pretty important in C++/CLI to know when to use the ^ hat. Which is what you are struggling with here, DateTime
is a value type and variables of that type should not be declared as references. Just like you'd never write int^ i = 42;
. A bit sad that the compiler accepts it anyway, it produces a boxed value. 99.9% of the time that is not what you want, boxing doesn't come for free. You'll dig a much deeper hole when you then try to use it in casts.
Sample C# code:
namespace ClassLibrary45
{
public struct Julian {
public static explicit operator Julian(DateTime rhs) {
return new Julian();
}
}
}
Used in sample C++/CLI code:
using namespace System;
using namespace ClassLibrary45;
int main(array<System::String ^> ^args)
{
DateTime dt = DateTime::Now;
Julian j = (Julian)dt;
return 0;
}
Oops, I did it backwards. Well, you get the idea.