Using an object created with Assembly.CreateInstance()

Posted on 2007-10-04
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.
Question by:Paracom_Inc
    LVL 22

    Expert Comment

    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


    Author Comment

    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();
    LVL 22

    Expert Comment

    Set a breakpoint on line 5 and see what you have in myObject.

    LVL 2

    Accepted Solution

    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:

    I hope this helps...


    Author Comment

    Yes, the interface route is the way that I have done it as well. Thanks for the confirmation.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now