Dynamically Loading Assembly and Accessing its Types (namespaces are different)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Smithers

    #1

    Dynamically Loading Assembly and Accessing its Types (namespaces are different)

    Using 3.5, I am stuck in attempting to:

    1. Dynamically load an assembly
    2. Instantiate a class from that assembly (the client code is in a different
    namespace than the namespace of the dynamically loaded assembly)

    so far so good (per my code below)... but here is where I'm getting hung up:
    3. Call methods of that type (see comments in my code)

    If the types in the dynamically loaded assembly were in the same namespace
    as the namespace of the code that does the loading, then life would be good.
    I could just define an interface and implement it in the dynamically loaded
    type. Then the client code could operate on the loaded type as its interface
    type. But the namespaces are different (and they need to stay different -
    and there cannot be a project reference to the dynamically loaded type's
    project).

    How do I get around that? How can I call methods of the type when (1) the
    client code is in a different namespace and (2) the type gets loaded at
    runtime - thus no ability to cast to the necessary type?

    //*************** *************** *************** *************** *************
    string assemblyName = "CompanyNameHer e.CopyFileInsta ller";
    string typeName = "CompanyNameHer e.Installers.Co pyFileInstaller "; //
    includes namespace

    System.Reflecti on.Assembly loadedAssembly =
    System.Reflecti on.Assembly.Loa d(assemblyName) ;

    Type theType = null;
    theType = loadedAssembly. GetType(typeNam e);

    object instance = null; // ultimately I don't want an object type here if
    possible
    if (theType != null)
    {
    instance = Activator.Creat eInstance(theTy pe); // this works.

    // ??? what goes here so I can call methods of the CopyFileInstall er type?
    // instance is of object - and I don't know how to cast to the Type
    of theType.
    // That type is defined a different namespace than this executing
    code
    // and that namespace is located in an assembly loaded only at
    runtime,
    // so I can't just set a project reference...
    }
    //*************** *************** *************** *************** *************

    Thanks!

    - "Smithers"


  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Dynamically Loading Assembly and Accessing its Types (namespaces are different)

    Hi,

    "Smithers" <A@B.comwrote in message
    news:uZ32Kff1HH A.5980@TK2MSFTN GP04.phx.gbl...
    Using 3.5, I am stuck in attempting to:
    >
    1. Dynamically load an assembly
    2. Instantiate a class from that assembly (the client code is in a
    different namespace than the namespace of the dynamically loaded assembly)
    >
    so far so good (per my code below)... but here is where I'm getting hung
    up:
    3. Call methods of that type (see comments in my code)

    It has nothing to do with the namespace with rather the definition of the
    interfaces of those types dynamically loaded.
    If those types implement an interface known by the calling code you are ok,
    you can cast the CreateInstance to the correct type.
    For example if any type implement System.Windows. Form you can access it.

    A possible solution in your case is to define a set of interfaces in a
    separate DLL that both the calling code as the called code types implement.
    Then you can access them using these interfaces.


    Comment

    • Smithers

      #3
      Re: Dynamically Loading Assembly and Accessing its Types (namespaces are different)

      RE:
      << A possible solution in your case is to define a set of interfaces in a
      separate DLL that both the calling code as the called code types implement.
      Then you can access them using these interfaces.>>

      I just did this and it works beautifully.

      A quick followup question:
      Is doing the above considered in any way to be a "hack" or somehow a "bad
      thing"?

      While it creates another project and .dll to maintain (as little as that
      maintenance is over time), it seems to be *less* of a hack than using
      Reflection (which I also got to work) - at least in my "plugin application".
      I'd be interested in your thoughts on this perspective.

      Thanks again.



      Comment

      Working...