时间:2019-04-14 标签:c#4.0DLR和COM

发布于 2025-01-07 02:24:24 字数 275 浏览 0 评论 0原文

说明 DLR 优点的一个常见示例是将其与 COM 等遗留组件一起使用,并且能够调用在编译时不可见的方法。

跳过 COM Interop 步骤有什么意义吗?因为一旦组件被重新压缩,编译器就会拥有元数据。

假设我们确实跳过了 Interop 步骤,对 COM 签名(由 .net 使用)的任何更改仍然需要重新编译。

如果我不得不做出另一个猜测,DLR 还提供调用缓存,因此任何后续调用都应该比使用常规反射的调用更快.. 所以也许这是 DLR 的一个好处.. 但从语法上讲,我只是没有看到它..

a common example to illustrate the benefits of DLR is to use it with legacy components such as COM and having the ability to call methods that are not visible at compile time.

is the point of this to skip the COM Interop step? Because once the component has been Regasm'd the compiler will have the metadata.

and let's say we do skip the Interop step, any changes to COM signature (that is used by .net) will still require re-compilation.

if i had to make another guess, the DLR also provides caching of the calls, so any subsequent calls should be faster than those using regular reflection.. so maybe that's one benefit of the DLR here.. but syntactically i'm just not seeing it..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尤怨 2025-01-14 02:24:24

这样做的目的是跳过 COM Interop 步骤吗?

不,它只是简化了方法的调用,而这对于反射来说可能是相当痛苦的。无论您使用Reflection还是C# 4.0动态,都使用Interop与非托管代码进行通信。

假设我们确实跳过了 Interop 步骤,即对 COM 的任何更改
签名(.net 使用的)仍然需要重新编译。

这取决于。如果重命名 COM 方法,则需要重新编译 .NET 调用代码,因为当您尝试调用不存在的方法时,它将在运行时中断。

is the point of this to skip the COM Interop step?

Not, it just simplifies the calling of methods which could be quite a pain with Reflection. No matter whether you use Reflection or C# 4.0 dynamic, both use Interop to communicate with the unmanaged code.

and let's say we do skip the Interop step, any changes to COM
signature (that is used by .net) will still require re-compilation.

It depends. If you rename a COM method you will need to recompile the .NET calling code because it will break at runtime when you attempt to call an non-existing method.

染柒℉ 2025-01-14 02:24:24

它对于与后期绑定 COM 进行互操作特别有用。不添加对 COM 类型库或 PIA 的引用的类型。在 dynamic 关键字可用之前,在 C# 中执行此操作非常痛苦,它需要反射代码。如果您有一个类型库,那么您几乎总是想使用它,因为它速度快得多,可以在编译时捕获错误并支持 IntelliSense。您不想使用它的唯一原因是当您尝试使代码足够灵活以处理不同版本的 COM 服务器时。然而,这是相当危险的,运行时异常总是随时会毁掉你的一天。事实上,没有类型库的情况很少见,几乎所有 COM 组件作者都提供类型库,因为它的优点非常大。

Regasm 恰恰相反,只有当您用 C# 编写自己的 [ComVisible] 服务器时才使用它。那么动态就没有任何好处了。

It is specifically useful to interop with latebound COM. The kind where you don't add a reference to a COM type library or PIA. Very painful to do in C# before the dynamic keyword became available, it requires reflection code. If you have a type library then you almost always want to use it since it is a lot faster, catches mistakes at compile time and supports IntelliSense. The only reason you would not want to use one is when you try to make your code flexible enough to handle different versions of the COM server. It is however quite risky with a runtime exception always around the corner to ruin your day. It is in fact rare to not have a type library, almost all COM component authors provide one since the advantages are so great.

Regasm is the exact opposite, you only use that when you write your own [ComVisible] server in C#. There's no benefit to dynamic then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文