Click here to Skip to main content
13,836,827 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


50 bookmarked
Posted 2 Feb 2010
Licenced CPOL

Embed an Assembly as a Resource

, 2 Feb 2010
Rate this:
Please Sign up or sign in to vote.
Embed an assembly as a resource, exemplified with Log4Net


If your application uses another dependent assembly, you have to deploy this assembly also together with your application. In some cases however, you wish you could only deploy your application assembly – and nothing else. To achieve this, you could embed the dependent assembly as a resource into your application assembly.

Unfortunately, this is not the whole story. You also need some other things, which I will describe in the following. As an example, I will use the embedding of log4net.dll.

I assume you already have an application using log4net. If not, it is a good idea to first create the application with the complete functionality. Not before nearly the end, embed the dependent assembly.

Embed the Resource

The start is quite intuitive: add a folder Resources to your project, add the log4net.dll to this folder, and change its build action to Embedded Resource.

Load the Assembly

The next step is to load the assembly from the resources. This must be done before the first use of the assembly.

First, you must determine the name of the resource:

Assembly assembly = Assembly.GetExecutingAssembly();
string assemblyName = assembly.FullName.Split(',')[0];
string resourceName = assemblyName + ".Resources.log4net.dll";

Now you can load the assembly:

Stream imageStream = assembly.GetManifestResourceStream(resourceName);
long bytestreamMaxLength = imageStream.Length;
byte[] buffer = new byte[bytestreamMaxLength];
imageStream.Read(buffer, 0, (int)bytestreamMaxLength);
log4NetAssembly = Assembly.Load(buffer);

Remove the Reference

Until now, you have gained nothing. Because log4net.dll is still referenced in your project, the .NET loader tries to load it together with your assembly. Unfortunately, the .NET loader does not know that you embedded the log4net.dll in your assembly. Therefore, it produces an error during the load of your assembly:

Could not load file or assembly ' ... ' or one of its dependencies. 
The system cannot find the file specified.

This happens before the code in your assembly starts. Therefore, you have no chance to change this behavior. The only chance is to remove the reference from your assembly.

Create a Proxy

As soon as you remove the reference, you will not be able to compile your application anymore (which solves the deployment problem, albeit in an unwished way). This is quite clear, because now the compiler does not know what to do with your log4net calls.

To make your application compilable again, you have to add your own log4net classes, which call the original log4net assembly via reflection. Fortunately, you have to implement only these classes and methods you use.

A good idea is to create a new folder called Log4NetProxy. To this folder, you can add your own implementation for every needed class.

A typical use of log4net looks like this:

ILog logger = LogManager.GetLogger(typeName);

In your proxy, now you need implementations for:

  • ILog
  • LogManager
  • LogImpl (which is returned by LogManager.GetLogger() and implements ILog)

Additionally, you should use a utility class called Loader.


The loader class should have a static variable, which contains the loaded assembly (as described above). Additionally, it should contain a helper method to get a specific type from the assembly:

static class Loader
  private static readonly Assembly log4NetAssembly = LoadAssembly();

  static static Assembly LoadAssembly()
    return Assembly.Load(buffer);

  internal static Type GetType(string typeName)
    Type type = null;
    if (log4NetAssembly != null)
      type = log4NetAssembly.GetType(typeName);
      if (null == type)
        throw new ArgumentException("Unable to locate type");
    return type;


The interface is quite simple. Just add all members from the original interface, which you use. For the example above, the following code is enough:

interface ILog
  void Info(object message);


From the LogManager’s methods, you also need only one method. It first gets the LogManager type from the loaded assembly, and then it invokes the method GetLogger via reflection. Finally, the returned object will be encapsulated in LogImpl. The last step is necessary, because otherwise the using client (your application) would have to use reflection itself. But you want to leave your code as less changed as possible.

Overall, you get the following code:

static class LogManager
  public static ILog GetLogger(string name)
    ILog logger = null;
    Type logManager = Loader.GetType("log4net.LogManager");
    if (logManager != null)
      MethodInfo method = logManager.GetMethod("GetLogger", 
                            new[] { name.GetType() });
      logger = new LogImpl(method.Invoke(null, new[] { name }));
    return logger;


LogImpl also uses reflection to call the original implementation. Because it has instance methods, the real instance is a parameter to the constructor:

class LogImpl : ILog
  private readonly object instance;

  internal LogImpl(object instance)
    this.instance = instance;

  public void Info(object message)
    MethodInfo method = instance.GetType().GetMethod("Info", 
                          new[] { message.GetType() });
    method.Invoke(instance, new[] { message });

Use the Proxy

The rest is quite easy: in every class, you have a compile error because of the missing log4net assembly, replace the statement:

using log4net;


using Log4NetProxy;

Handle the AssemblyResolve Event

For the most scenarios, the so far outlined solution should work. However, sometimes assemblies are also referenced in another way. In the case of log4net, this is its configuration, which looks like:

  <appender name="MyAppender" type="log4net.Appender.ConsoleAppender">

The XML above will be parsed from inside of log4net. However, it will try to load the type log4net.Appender.ConsoleAppender from the assembly log4net. Because the .NET loader does not know anything about our own loading, it looks itself for the log4net assembly. Again, we get:

Could not load file or assembly 'log4net' or one of its dependencies. 
The system cannot find the file specified.

To prevent this, you can give the loader a hint by hooking the AssemblyResolve event:

AppDomain.CurrentDomain.AssemblyResolve +=
  delegate(object o, ResolveEventArgs args)
    if (args.Name == "log4net")
      return log4NetAssembly;
      return null;

The best place for this piece of code is immediately after the successful loading of the assembly.


  • 31-Jan-2010
    • Initial public release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

SuggestionInteresting approach Pin
Kelvin3329-Oct-11 16:47
memberKelvin3329-Oct-11 16:47 
QuestionCould be simpler? Pin
BigMetsFan22-Aug-10 17:31
memberBigMetsFan22-Aug-10 17:31 
GeneralExcellent - and only the begining Pin
alex turner2-Feb-10 23:22
memberalex turner2-Feb-10 23:22 
GeneralExepack.NET Pin
Huisheng Chen2-Feb-10 23:22
memberHuisheng Chen2-Feb-10 23:22 
GeneralMore simple way with ILMERGE Pin
Olaf Herrmann2-Feb-10 22:50
professionalOlaf Herrmann2-Feb-10 22:50 
GeneralILMerge Pin
fmaeseele2-Feb-10 22:48
memberfmaeseele2-Feb-10 22:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190114.1 | Last Updated 3 Feb 2010
Article Copyright 2010 by Ritzlgrmft
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid