在C#.net 4.0中在运行时加载TLB [英] Load TLB at runtime in C# .net 4.0

查看:215
本文介绍了在C#.net 4.0中在运行时加载TLB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方图书馆的tlb文件.该库有很多版本,但是我在tlb中使用的功能是不变的,即不会从一个版本更改为另一个版本.

I have a tlb file from a third party library. There are many versions of this library, however the functions that I use within the tlb are constant i.e. do not change from one version to the next.

我已将tlb文件作为COM参考添加到项目中.我可以成功构建并调用第三方库中的函数.

I have added the tlb file to the project as a COM Reference. I can successfully build and call the functions from the third party library.

当我部署应用程序时,我不能保证用户的PC上将安装与我相同的第三方版本.在这种情况下,一旦调用第三方库中的函数,他们就会收到访问冲突错误(0xc0000005).

When I deploy my application, I cannot guarantee that the user will have the same version of the third party installed on their PC as me. When this scenario arises, they get an access violation error (0xc0000005) as soon as a function within the third party library is called.

如何在运行时加载位于用户PC上的tlb文件?

How can I load the tlb file that is located on the user's PC at run-time?

推荐答案

您应该能够将C#4的动态功能与

You should be able to use C# 4's dynamic along with Type.GetTypeFromProgID to load the COM object and use it via dynamic binding.

 dynamic obj = Activator.CreateInstance(Type.GetTypeFromProgID(comTypeName));

 // Use object via dynamic binding directly
 obj.Foo();

这避免了将有关COM对象版本的任何内容编译到程序集中的需要.只要您使用正确的API,并且您使用的方法/等存在,它便可以在任何版本中使用.

This avoids the need for anything about the COM object version to be compiled into your assembly. As long as you use the correct API, and the methods/etc you use exist, it will function with any version.

这篇关于在C#.net 4.0中在运行时加载TLB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆