?
Solved

Exception thrown when calling Assembly.CreateInstance Method

Posted on 2005-03-28
4
Medium Priority
?
332 Views
Last Modified: 2010-04-16
hi,

i have 2 assemblies which both has a class name Source.
i compile them seperately and place the dlls in the target application directory.
lets call them ass1.dll and ass2.dll.

i want to create an instance of those 2 objects so i use Assembly.CreateInstance to do so, as follow:

string [] strFiles = System.IO.Directory.GetFiles(System.IO.Directory.GetParent(strCurrentDirectory)+"//Services");
foreach(string strFile in strFiles)
{
      Assembly asm = Assembly.LoadFile(strFile);
foreach(Type t in asm.GetTypes())
{
//i look for the "source" class
if(t.Name != "Source")
continue;

object obj = null;
                                                try
                                                {
//t.ToString() contains namespace + class name

                  obj = asm.CreateInstance(t.ToString(), true,                                                                              BindingFlags.Public | BindingFlags.Instance,                                                                              null, new object[]{/*some argument*/}, null, null);

                                                }
                                                catch(Exception e)
                                                {
                                                      string str = e.Message;
                                                }
}
}

the exception massage is:
{"Item has already been added.  Key in dictionary: \"Source\"  Key being added: \"Source\"" }

what that means?

why i always get an exception in the second time when i'm trying to create the instance, despite the fact that the class is from a different namespace?


cheers
0
Comment
Question by:sedgwick
[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
  • 3
4 Comments
 
LVL 13

Accepted Solution

by:
dungla earned 300 total points
ID: 13642636
when you call asm.CreateInstance it will throw exception or when you try to add a value to hashtable?
0
 
LVL 42

Author Comment

by:sedgwick
ID: 13642790
on CreateInstance()

the hastable code is being performed in case the CreateInstance succeed.
0
 
LVL 42

Author Comment

by:sedgwick
ID: 13643031
i was trying to use tyhe following code instead but i got the same exception:

ObjectHandle objh = Activator.CreateInstanceFrom(strFile, t.ToString());
Object obj = objh.Unwrap();
0
 
LVL 42

Author Comment

by:sedgwick
ID: 13643191
i found the problem,

the instance being created is a property grid derived class which register itself in a singleton property manager which holds a hashtable (thats why the exception with the dictionary key).

10x for the help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
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…
Suggested Courses

765 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