C++/CLI 中使用的动态 C# 对象
我目前正在构建一个 c++/cli 互操作库。该库将由 c# 和 vb.net 应用程序使用,我想利用 .Net 4 动态类型。我有一个配置模块,它使用动态成员访问来引用配置键,这在 c# 中工作正常,但在 c++/cli 中给我带来麻烦(并非完全出乎意料) 有没有办法在 C++/CLI 中使用这个库(例如通过直接调用 TryGetMember() 或其他方式)?如果 C++/CLI 库随后将动态配置传递给用 C# 编写的派生类,那么 C# 仍然能够使用动态成员访问吗?请注意,基类是在 c++/cli 中定义的,因此我无法使用dynamic关键字。 (或者我会吗?)
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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个开源库,ImpromptuInterface(通过 nuget 找到),当涉及动态关键字时,它简化了 c# 编译器使用的 api,我假设它在 c++/cli 中工作,因为它们只是静态 c# 函数。我知道它可以在 F# 中运行,但也没有 DLR 支持。
以下是它可以执行的动态调用的列表。
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.
C#/VB 动态类型由编译器转换为普通类型和方法调用。生成的 IL 有点复杂,但无论如何也不特殊。您绝对可以编写生成类似 IL 的 C++/CLI 代码。
也许编写 C++/CLI 代码的最简单方法是在 C# 中编写使用动态类型的代码,然后使用 Reflector 将程序集反编译为 C++/CLI。
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.