Advertisement

10.04.2007 at 02:44PM PDT, ID: 22873492
[x]
Attachment Details

Using an object created with Assembly.CreateInstance()

Asked by Paracom_Inc in Microsoft Visual C#.Net, Reflection for .Net, .NET Framework 2.0

Tags: assembly

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.Start Free Trial
[+][-]10.04.2007 at 03:02PM PDT, ID: 20018279

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.05.2007 at 06:33AM PDT, ID: 20021677

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.05.2007 at 08:43AM PDT, ID: 20022795

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.05.2007 at 10:01AM PDT, ID: 20023365

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, Reflection for .Net, .NET Framework 2.0
Tags: assembly
Sign Up Now!
Solution Provided By: jdbviper
Participating Experts: 2
Solution Grade: A
 
 
[+][-]10.08.2007 at 08:39AM PDT, ID: 20034781

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_1_20070628