Solved

Using DLLs added as a resourcein C#

Posted on 2012-03-18
9
316 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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