Solved

Dynamically Loading and calling a .net object

Posted on 2003-10-30
5
372 Views
Last Modified: 2008-03-04
Is it possible to dynamically load then call a .net object. For instance, I have a exe and with this exe, I want to provide a folder that has a bunch of .net dlls with the same interface. I don't know the names of these dlls before the exe is compiled. I want to load all of the .net objects into memory and let the user select which ones to use.
0
Comment
Question by:skibud2
  • 2
  • 2
5 Comments
 
LVL 10

Assisted Solution

by:ptmcomp
ptmcomp earned 125 total points
ID: 9654174
Yes, I've also done this already.
Just use:
Type myType = Type.GetType("Full.Class.Name, AssemblyName");
Activator.CreateInstance(myType);
((IMyInterface)myType).MyMethod();
0
 

Author Comment

by:skibud2
ID: 9654682
doesn't that assume that it is already loaded in the GAC? I am trying to load it from the dll.
0
 
LVL 6

Accepted Solution

by:
etmendz earned 125 total points
ID: 9654996
Interesting project. ptmcomp's suggestion is close. You can read about the following and decide on what serves your purpose best:

Assembly.Load()
Assembly.LoadFrom()
Activator.CreateInstance()
Activator.CreateInstanceFrom()
Activator.CreateComInstanceFrom()

Have fun...
0
 

Author Comment

by:skibud2
ID: 9655198
I split it 125/125 because you both gave answers that helped me. Thanks guys!

-Mike
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9656129
That's the code I used:
                  foreach (string typeName in typeNames)
                  {
                        Type type;
                        object dataSource;

                        type = Type.GetType(typeName);
                        if (type == null)
                        {
                              throw new ApplicationException(string.Format("Cannot load type '{0}'", typeName));
                        }
                        dataSource = Activator.CreateInstance(type);
                  }

This way there is no need to call Assembly.Load()
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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