explicit typecasting of a C# class in C++/CLI

9a3eedi

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?

Hans Passant
   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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

explicit typecasting of a C# class in C++/CLI

From Dev

TypeCasting Map <int, class A> to <int, class B> in C++

From Dev

Typecasting in division in C

From Dev

Typecasting a double pointer in C

From Dev

Typecasting error in C

From Dev

Typecasting Pointers in C

From Dev

Typecasting of pointers in c

From Dev

C# How to call the base class == operator, without explicitly typecasting?

From Dev

Difference in explicit and non-explicit C# class initializers

From Dev

Typecasting integer to pointer to integer in c

From Dev

How is typecasting parsed by C compilers?

From Dev

Typecasting a negative number to positive in C

From Dev

Explanation of typecasting of pointer in C++

From Dev

pointer typecasting like c in python

From Dev

Typecasting a negative number to positive in C

From Dev

Typecasting from C++ to Delphi

From Dev

Wrapping C++ CLI Class For C#

From Dev

Wrapping C++ CLI Class For C#

From Dev

C# how to avoid typecasting to subclass?

From Dev

c++ Need for typecasting int to unsigned int

From Dev

Typecasting C pointers to access raw memory

From Dev

Can a C structure ever interfere with typecasting?

From Dev

Typecasting C pointers to access raw memory

From Dev

C: Performant temporary variables to avoid frequent typecasting

From Dev

c++ emulate typecasting using assignment operator

From Dev

Calculation on pixel values and typecasting c#

From Dev

Typecasting a struct to unsigned char* in c++

From Dev

C++ template class explicit instantiation failing with GCC/NDK

From Dev

C++: explicit constructor is implicitly called by derived class