Dynamic C# object used in C++/CLI

﹥>﹥吖頭↗ 提交于 2019-12-10 18:03:33

问题


I'm currently building a c++/cli interop library. The library will be consumed by c# and vb.net applications, and I would like to make use of .Net 4 dynamic typing. I have a configuration module that uses dynamic member access to refer to configuration keys, this works fine in c# but is giving me trouble in c++/cli (not entirely unexpected) Is there any way to use this library in C++/CLI (e.g. by invoking TryGetMember() directly or something)? If the C++/CLI library then passes the dynamic configuration to a derived class written in C#, will the C# still be able to use dynamic member access? Note that the base class is defined in c++/cli, so I wouldn't be able to use the dynamic keyword. (or would I?)


回答1:


There is an open source library, ImpromptuInterface (found via nuget) that simplifies the api's used by the c# compiler when the dynamic keyword is involved, I'd assume it works in c++/cli, since they are just static c# functions. I know it works in F# that also doesn't have DLR support.

Here is a list of dynamic invoking it can do.




回答2:


C#/VB dynamic types are translated by the compiler into ordinary types and method calls. The generated IL is somewhat complex, but not special in any way. You can definitely write C++/CLI code that generates similar IL.

Perhaps the easiest way to write the C++/CLI code would be to write the code that uses the dynamic types in C#, and then use Reflector to decompile the assembly into C++/CLI.



来源:https://stackoverflow.com/questions/9283731/dynamic-c-sharp-object-used-in-c-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!