1

generally we often add reference of dll and then access classed inside that dll and create instance of that classed. now i have include a dll file in my project as embeded resource. now my question is that how could i access classes and create instance of that classes which is there in that dll which is included as embeded resource. i search google and found stackoverflow link like Embedding one dll inside another as an embedded resource and then calling it from my code

the instruction i found there for accessing dll which is included as embeded resource like

Once you've embedded the third-party assembly as a resource, add code to subscribe to the AppDomain.AssemblyResolve event of the current domain during application start-up. This event fires whenever the Fusion sub-system of the CLR fails to locate an assembly according to the probing (policies) in effect. In the event handler for AppDomain.AssemblyResolve, load the resource using Assembly.GetManifestResourceStream and feed its content as a byte array into the corresponding Assembly.Load overload. Below is how one such implementation could look like in C#:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
var resName = args.Name + ".dll";    
var thisAssembly = Assembly.GetExecutingAssembly();    
using (var input = thisAssembly.GetManifestResourceStream(resName))
{
    return input != null 
         ? Assembly.Load(StreamToBytes(input))
         : null;
}
};

where StreamToBytes could be defined as:

static byte[] StreamToBytes(Stream input) 
{
var capacity = input.CanSeek ? (int) input.Length : 0;
using (var output = new MemoryStream(capacity))
{
    int readLength;
    var buffer = new byte[4096];

    do
    {
        readLength = input.Read(buffer, 0, buffer.Length);
        output.Write(buffer, 0, readLength);
    }
    while (readLength != 0);

    return output.ToArray();
}
}

few things was not clear to me that. the person said

add code to subscribe to the AppDomain.AssemblyResolve event of the current domain during application start-up. This event fires whenever the Fusion sub-system of the CLR fails to locate an assembly according to the probing (policies) in effect.

what is Fusion sub-system of the CLR fails? what does it mean? when AssemblyResolve event will fire. do i need to put this code in my program.cs file.

Assembly.Load() will just load assembly into memory but they did not show how to create instance of classed inside that dll?

please discuss my points of interest in detail. thanks

Community
  • 1
  • 1
Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

1

What is Fusion sub-system of the CLR fails? what does it mean?

This article explains it in detail (especially the part Probing for the Bits (Fusion)):

When probing is unable to locate an assembly, it will trigger the AppDomain.AssemblyResolve event, permitting user code to perform its own custom loading. If all else fails, a TypeLoadException is thrown (if the load process was invoked due to a reference to a type residing in a dependent assembly) or a FileNotFoundException (if the load process was invoked manually).


Assembly.Load() will just load assembly into memory but they did not show how to create instance of classed inside that dll?

Another question in SO explains how to create an instance of a type in a dynamically loaded assembly.

Community
  • 1
  • 1
tafa
  • 7,146
  • 3
  • 36
  • 40