Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamically Loading and calling a .net object

Posted on 2003-10-30
5
Medium Priority
?
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 10

Assisted Solution

by:ptmcomp
ptmcomp earned 500 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 500 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

Hire Technology Freelancers with Gigs

Work with 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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