Solved

Using DLLs added as a resourcein C#

Posted on 2012-03-18
9
317 Views
Last Modified: 2012-03-22
I want to use a DLL I made in C# (I believe that means it is managed code) in a project I'm doing, but I want a single EXE after building. I added the DLL as a resource, but don't know how to access the objects or functions in the DLL.
0
Comment
Question by:byt3
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 37736967
Using DLL Component as Embedded Resource (Using Reflection)
http://www.codeproject.com/Articles/15253/Using-DLL-Component-as-Embedded-Resource-Using-Ref
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37737251
You should be able to use the DLL as an Embedded Resource, and reflection to call the Assembly.GetManifestResourceStream, which will return the assembly in a stream.  Use a stream reader to get the assembly bytes.  

Load the assembly, using the Assembly.Load(byte[]) method:

Assembly.Load Method (Byte())
http://msdn.microsoft.com/en-us/library/h538bck7.aspx
0
 
LVL 2

Author Comment

by:byt3
ID: 37737894
Thank you for the posts, I'll try this out later today to make sure I can understand the links you sent me.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 37738387
Microsoft as a tool to merge 2 assemblies (a .exe is an assembly, a .dll is an assembly) into one. You will find it there:

http://www.microsoft.com/download/en/details.aspx?id=17630
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37739130
Keep in mind that loading an assembly with reflection, you would need to use either more reflection to execute the methods, or dynamic keyword (C#).  IL Merge is a good suggestion, but can be very messy if you have a lot of references, since everything needs to be bound statically when merging at the IL level.  I have seen a simple set of references balloon out to hundreds of indirect assemblies.
0
 
LVL 40
ID: 37739386
An easier solution could be to simply bring the code source of your dll into the application project.

As for most techniques in programming, this is really a project by project decision. Some projects will work better with one solution, some projects will be better of with another technique. You have to see how your stuff was built, how you want to do it and also take into consideration the future maintenance of the project.

One question that has not been asked however, is why do you really want to do that?

If somebody took the decision of putting those classes in a dll first, they had a reason. If you want to send everything as one unit, there is also a reason. Sometimes, the decisions take in regard to go exe+dll vs exe only are taken because the designer does not know of certain techniques. For instance, I see a lot of programmers that wants a merge simply because they see a security problem by sending a dll, thinking that everything must be Public in a dll in order to be used from outside. This is not true.
0
 
LVL 2

Author Comment

by:byt3
ID: 37747840
Dhaest,
I don't understand Reflection enough to be able to work with that link I think.  If there is a link you can provide to explain it I'd be happy to read and hopefully figure this out.
0
 
LVL 2

Author Comment

by:byt3
ID: 37747899
TheLearnedOne,
I did find an article explaining what you're talking about I think:
http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

The thing is I can't initialize an object in my code from that dll and have it compile without errors being thrown:

The type or namespace name 'ClassInDLL' could not be found ...

I must have to additionally use reflection or something to create an instance of that object  and build it or something...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37748870
You should investigate the ILMerge suggestion, also, as it can definitely be a viable solution for a small number of external assembly references.  You should be able to reference the assembly as if it were external, but it would be internal to your executing assembly.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Release Dynamically Allocated Memory in C# 3 38
JSON Deserialize issue 6 26
SSIS On fail action 5 38
Adding items to a C# list incrementally 5 36
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question