Solved

Using DLLs added as a resourcein C#

Posted on 2012-03-18
9
315 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now