How can I generate C# source code for a COM Interop library instead of an assembly?

只谈情不闲聊 提交于 2019-12-10 15:49:44

问题


When importing a COM library (either directly with tlbimp, or indirectly with visual studio add reference dialog,) is there a way to generate C# source code instead of a binary interop assembly, like Interop.Word.dll, for example?

UPD: Reflector is bad idea. Problem is that for the com interface is not only a signature, but the order of the members. Reflector this order is violated


回答1:


I would go ahead and generate the interop assembly using TLBIMP, and the use Reflector to disassemble it. The interop assembly has no actual implementation code, as you will see. You can then just copy and paste the code (or the CoClasses and interfaces you need) into a new .cs file in your project.




回答2:


You cannot generate C# directly, but if your real problem is that you have a need to tweak the interop library then you might have better luck using the CLR Interop team's custom TLBIMP tool on codeplex - it's pretty flexible and you have full source [to it.]

http://clrinterop.codeplex.com/releases/view/17579

Update: If you're really trying avoid shipping binaries, then you could feasibly integrate the above tool (in source format) into your project so that you generate this interop library at runtime. Result? No binaries to ship.




回答3:


In general it cannot be done. C# does not support all possible constructs needed to create a working interop assembly. You cannot add e.g. attributes to return types or method parameters in C# although they are needed sometimes to help the marshaller to correctly translate some C++ types.

Yours, Alois Kruas



来源:https://stackoverflow.com/questions/6202965/how-can-i-generate-c-sharp-source-code-for-a-com-interop-library-instead-of-an-a

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