[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using an object created with Assembly.CreateInstance()

Posted on 2007-10-04
5
Medium Priority
?
2,557 Views
Last Modified: 2013-12-17
I am writing a C# application in which I need to load an assembly from a file and then create an instance of a specific type from that assembly. I'm using the following code to do this:

System.Reflection.Assembly a = System.Reflection.Assembly.LoadFile(path);
object myObject = a.CreateInstance("MyType");

The problem is that I then want to interact wth myObject as an instance of MyType:

MyType myInstance = myObject as MyType;

After this line of code myInstance is null. The VS debugger Autos pane shows the value of myObject as {MyType} and shows its Type as object {MyType}. Nonetheless, I cannot successfully cast myObject as MyType. Am I doing something wrong? Is there a way around this? Thanks.
0
Comment
Question by:Paracom_Inc
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20018279
This version of CreateInstance works for me:
CreateInstance (
      string typeName,
      bool ignoreCase,
      BindingFlags bindingAttr,
      Binder binder,
      Object[] args,
      CultureInfo culture,
      Object[] activationAttributes
)

typeName needs to be fully qualified; i.e. YourNameSpace.YourClass
ignoreCase - I send false
bindingAttr = BindingFlags.CreateInstance
binder = null
args = an array of objects for the constructor - can be empty or null for the default constructor
culture = null - uses currentCulture
activationAttributes = null

Jim
0
 

Author Comment

by:Paracom_Inc
ID: 20021677
I've tried that overload as well. The code is listed below. Line 6 fails because myInstance is null. Any ideas?

1         string path = @"C:\TestLibrary\bin\Debug\TestLibrary.dll";
2        System.Reflection.Assembly a = System.Reflection.Assembly.LoadFile(path);
3         string typeName = "TestLibrary.TestClass";
4         object myObject = a.CreateInstance(typeName);
5         TestLibrary.TestClass myInstance = myObject as TestLibrary.TestClass;
6         string s = myInstance.Hello();
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20022795
Set a breakpoint on line 5 and see what you have in myObject.

Jim
0
 
LVL 2

Accepted Solution

by:
jdbviper earned 2000 total points
ID: 20023365
I've only gotten this to work by using an interface to the class you want to create an instance of. Unfortunately, you have to put that interface into a separate assembly, and reference that assembly in your other 2 projects.

See the following question, maybe it will shed some light on your issue:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21482123.html

I hope this helps...

Jim
0
 

Author Comment

by:Paracom_Inc
ID: 20034781
Yes, the interface route is the way that I have done it as well. Thanks for the confirmation.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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