6

I have an application, written in C#, which needs some functionality from VB.NET (better said, something valid in CIL but not provided in C#).
So I have an executable and a library file.

Problem:
It has to be published in one file and may not be split to different assemblies!
But:
It's a WPF application, ILMerge will not work.

What could I do?
Is it possible to generate (performant) IL on the fly?

ordag
  • 2,497
  • 5
  • 26
  • 35
  • Oh, and I may not rewrite all code to `VB` ;-) – ordag Oct 27 '11 at 13:57
  • 1
    What functionality from VB.NET? – Chris Shouts Oct 27 '11 at 13:59
  • @ChrisShouts: Exception filters – ordag Oct 27 '11 at 14:01
  • I think we may need a little more detail to give a good answer. There are some post compilation tools that combine your assemblies int one file (www.spoon.net) but I'm not sure which one to recommend. You can also use ILMerge to do this (http://www.hanselman.com/blog/MixingLanguagesInASingleAssemblyInVisualStudioSeamlesslyWithILMergeAndMSBuild.aspx) – Mark Ewer Oct 27 '11 at 14:04
  • 1
    @MarkEwer: ILMerge for WPF is not possible. I need an exception filter in C#, and I have something written in VB.NET to provide that. – ordag Oct 27 '11 at 14:06
  • 2
    What makes more sense: generating IL on the fly or **giving up exception filters** and writing the equivalent code manually in C#? – Chris Shouts Oct 27 '11 at 14:06
  • @ChrisShouts: There is no equivalent code in C# from what I know. – ordag Oct 27 '11 at 14:08
  • 2
    Does this help (as a C# version of exception filters)? http://stackoverflow.com/questions/791390/more-elegant-exception-handling-than-multiple-catch-blocks/791479#791479 – George Duckett Oct 27 '11 at 14:09
  • @GeorgeDuckett: No, sry. I really need the `CIL` exception filters. That code translation is not the same. – ordag Oct 27 '11 at 14:12
  • 1
    Possibly stupid question but have you seen [this](http://stackoverflow.com/questions/3371583/serious-trouble-with-ilmerge-and-net-4-0)? Relevant quote *"...I have seen used to combine .dlls in WPF is to simply add the dll as an embedded resource the the project and then programatically load the dll into the assembly..."* Would let you have one executable and keep the two assemblies separate? – Roman Oct 27 '11 at 14:12
  • @R0MANARMY: Oh, didn't know about that, thanks. I prefer a solution with generating IL, but otherwise I may take this. – ordag Oct 27 '11 at 14:14

3 Answers3

2

See this question (and various answers) for merging a WPF exe and library dll together:
Merging dlls into a single .exe with wpf

See this answer, for a C# version of Exception Filters (may not be applicable in your case, kept here for future reference):
More Elegant Exception Handling Than Multiple Catch Blocks?

Community
  • 1
  • 1
George Duckett
  • 31,770
  • 9
  • 95
  • 162
  • Didn't know there were WPF merging tools out there. Thanks. Do you know how I could generate IL from C#, though? Sadly can not use that C# rewrite of exception filters. – ordag Oct 27 '11 at 14:19
1

I suggest that you still create two assemblies during the build but then embed one into the other as a resource. This way, you can call Assembly.Load() and load that secondary assembly.

using (var stream = Assembly.GetExecutingAssembly().
    GetManifestResourceStream(resourceName)) 
{
      Byte[] assemblyData = new Byte[stream.Length];
      stream.Read(assemblyData, 0, assemblyData.Length);
      return Assembly.Load(assemblyData);
}

See http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx for details.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Mark Ewer
  • 1,835
  • 13
  • 25
1

You can have one assembly, but with multiple modules compiled in different languages. You would set up three projects:

  • VB.NET-specific code project
  • C# code project
  • EXE project

Now, you can't use Visual Studio to build this (AFAIK, but you might be able to fiddle with the project files/MSBUILD tasks to do this), but you would use the command-line compilers to create netmodules.

Using csc.exe (for C#) you would use the /target:module command-line parameter, while with vbc.exe (for VB.NET) you would also use the /target:module command-line parameter.

Then, when building your EXE project, you would use the command-line compiler (depending on the language) and use the /addmodule:<module> (assuming the EXE project is in C#) or the /addmodule:<module> (assuming the EXE project is in VB.NET) to point to the modules that you specified to include in the EXE output assembly.

You also have the option of compiling everything into modules, and using the Assembly Linker (Al.exe) to produce an output assembly.

If the above option doesn't appeal to you, then another option is to pick one language and use that.

If you use VB.NET, then you will have exception filters (which you indicated is the piece you needed, which is a language feature, not exposed through the framework).

If you use C#, you don't have exception filters, but you can emulate them as indicated by George Duckett's answer.

Community
  • 1
  • 1
casperOne
  • 73,706
  • 19
  • 184
  • 253
  • Seems I still need the module files in my executable folder. C#, different from VB.NET, does not have the CIL exception filters and sadly they can not fully be rewritten in C#. – ordag Oct 27 '11 at 14:44