What is the C # keyword equivalent in the C ++ CLI?

There are two projects, one C ++ CLI and the other C #.
The C # project has a link to the C ++ CLI project.

In C #, I want to do this:

//method signature is somemethod(dynamic data); somemethod("haaaii"); 

Now this method, which is in the C ++ CLI project, should handle this.

How to declare this method in C ++ CLI?
Also how to define data type in C ++ CLI?

+6
source share
2 answers

To get the method signature that C # sees as dynamic :

 void TestMethod( [System::Runtime::CompilerServices::DynamicAttribute] System::Object^ arg ) { } 

But if you just want to accept all types, you can just use System::Object^ . The attribute is misleading, as it implies semantics that are very difficult for you to provide.

To find out the actual data type, use arg->GetType() . You can then use all the power of reflection and / or DLR to detect and call elements at runtime.

It is a little more useful to use the attribute for the return type, since then C # will invoke the semantics of dynamic when using the var keyword.

 [returnvalue: System::Runtime::CompilerServices::DynamicAttribute] System::Object^ TestReturn( void ) { return 1; } 
+6
source

You may need to get the dynamic type System::Dynamic::DynamicObject

void somemethod(ref System::Dynamic::DynamicObject data) { }

0
source

All Articles