user595809
user595809

Reputation:

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

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

Upvotes: 4

Views: 3147

Answers (3)

Alois Kraus
Alois Kraus

Reputation: 13545

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

Upvotes: 0

Kevin Hsu
Kevin Hsu

Reputation: 1756

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.

Upvotes: 1

x0n
x0n

Reputation: 52430

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.

Upvotes: 0

Related Questions