[英]What is C# dynamic keyword equivalent in C++ CLI?
There are two project, one C++ CLI and other is C#. 有两个项目,一个是C ++ CLI,另一个是C#。
The C# project has reference to C++ CLI project. C#项目引用了C ++ CLI项目。
In C# I want to do this : 在C#中我想这样做:
//method signature is somemethod(dynamic data);
somemethod("haaaii");
Now the method which is in C++ CLI project must handle this. 现在C ++ CLI项目中的方法必须处理这个问题。
How to declare this method in C++ CLI ? 如何在C ++ CLI中声明此方法?
Also how to detect data type in C++ CLI ? 另外如何在C ++ CLI中检测数据类型?
To get a method signature which C# sees as dynamic
: 要获得C#视为
dynamic
的方法签名:
void TestMethod( [System::Runtime::CompilerServices::DynamicAttribute] System::Object^ arg )
{
}
But if you just want to accept all types, you can simply use System::Object^
. 但是如果你只想接受所有类型,你可以简单地使用
System::Object^
。 The attribute is misleading, as it implies semantics which you will have a very hard time providing. 该属性具有误导性,因为它暗示了您将很难提供的语义。
To discover the actual data type, use arg->GetType()
. 要发现实际数据类型,请使用
arg->GetType()
。 You can then use all the power of reflection and/or the DLR for discovering and invoking members at runtime. 然后,您可以使用反射和/或DLR的所有功能在运行时发现和调用成员。
Slightly more useful is to use the attribute on a return type, since then C# will infer dynamic
semantics when the var
keyword is used. 稍微有用的是在返回类型上使用该属性,因为当使用
var
关键字时,C#将推断dynamic
语义。
[returnvalue: System::Runtime::CompilerServices::DynamicAttribute]
System::Object^ TestReturn( void )
{
return 1;
}
您可能必须使用System::Dynamic::DynamicObject
类型获得动态
void somemethod(ref System::Dynamic::DynamicObject data) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.