modernzombie
modernzombie

Reputation: 2049

Class Library As Embedded Resource In A Class Library

I have a C# class library that compiles to a DLL. My class library also references another class library DLL.

I need to add the dependency DLL as an embedded resource so I only have 1 DLL file. This file is a plugin for software I have no control over and the software will not resolve the dependency DLL properly.

I have found example of adding class libraries to Windows Forms projects but cannot find how to add a class library as an embedded resource in a class library project.

Upvotes: 0

Views: 2008

Answers (1)

Ananke
Ananke

Reputation: 1230

Add the assembly to your project as a regular file rather than a reference at the root of the project (it can be anywhere but it's easier to put it there for this explanation).

Click on it in the solution explorer, and in the properties window set the 'Build Action' to 'Embedded Resource'.

You can use the following code snippet to load the assembly at runtime (remember to change the string passed into GetManifestResourceStream):

        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("TestProject.MyAssembly.dll"))
        {
            if (stream == null)
            {
                throw new InvalidOperationException("Cannot find resource.");
            }

            using (var reader = new BinaryReader(stream))
            {
                var rawAssembly = new byte[(int)stream.Length];

                reader.Read(rawAssembly, 0, (int)stream.Length);
                var assembly = Assembly.Load(rawAssembly);

                // Do what you want with the assembly
            }
        }

Upvotes: 0

Related Questions