Can I compile C# code and VB.NET code into the same DLL?

woaksi

Can I compile C# code and VB.NET code into the same DLL?

by woaksi » Thu, 03 Mar 2011 03:31:28

We have 2 libraries of code that we want to combine into a single library so that they both have access to each other. One is in VB.NET and one is in C#. Both are .NET 2.0 in Visual Studio 2005.

Thanks.

John.



cgrau

Can I compile C# code and VB.NET code into the same DLL?

by cgrau » Sat, 05 Mar 2011 04:32:29

No, but you can buy programs that convert between C# and VB.NET, in both directions.



rlarn

Can I compile C# code and VB.NET code into the same DLL?

by rlarn » Sun, 06 Mar 2011 06:34:31

John,

Next to the question if you can do this, is the question what is your further plan for these two libraries:

If in future you really want to merge the two into a single library and maintain the library as a single code base, your best bet is to go with the option of Christian. As the 'human' factor of having to maintain code in 2 different languages will cause most likely more problems than the technical issues.

If however the code base is to be maintained by two different teams, then indeed you can use the SDK tools to merge the two libraries into a single assembly.

Rudi



Mattias Sjogre

Can I compile C# code and VB.NET code into the same DLL?

by Mattias Sjogre » Mon, 07 Mar 2011 05:33:30

You can create a multi-module assembly where each module is created in a different language. Or you can use a tool that merges multiple assemblies into one.



Paul Harrington MSF

Can I compile C# code and VB.NET code into the same DLL?

by Paul Harrington MSF » Tue, 08 Mar 2011 07:35:32

Mixing languages in a single assembly is not supported in the Visual Studio IDE, but it can be achieved by hand-generating the necessary build steps.

First, you will also need to compile your separate pieces as "netmodules", instead of DLLs. For both the C# compiler (CSC.EXE), and the VB compiler (VBC.EXE) the switch is /target:module

The "merging" stage is done through the Assembly Linker tool, AL.EXE

I haven't tried this, but I also notice that both C# and VB compilers support the "/addmodule" switch. So you might be able to get away with using one, or the other to do the final merge instead of using AL.EXE



spott

Can I compile C# code and VB.NET code into the same DLL?

by spott » Wed, 09 Mar 2011 09:37:34

Netmodules are not supported directly in the VB IDE.

You can compile up a netmodule using the commandline compiler - VBC to create a netmodule. A netmodule is compiled to IL code but lacks the assembly metadata. So you can create netmodules in C# or VB.

Once you have the netmodule you can link these together into a class libary (DLL). The DLL itself contains the assembly metadata as well as the functionality of the netmodules. It seems like a lot of hardwork to achieve the result in most cases.

In the IDE, you can add a netmodule reference directly but you could reference the class libary which contains the netmodule code.

Its probably better to create separate class libaries in each language add the two references and if you ensure they have the same namespaces then when you add the references and use the functionality you wouldnt know which language was used to generate the functionality. Much easier approach and one which eliminates the manual processes required.


knockNro

Can I compile C# code and VB.NET code into the same DLL?

by knockNro » Thu, 10 Mar 2011 08:36:33