Dynamically loading assemblies and interfaces

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

    #1

    Dynamically loading assemblies and interfaces

    Given the following code....

    // FooBar.cs, builds Activate.dll

    using System;

    public interface IFoo
    {
    void Bar();
    }

    public class FooBar : IFoo
    {
    public FooBar()
    {
    }

    public void Bar()
    {
    Console.WriteLi ne( "FooBar" );
    }
    }
    // End of FooBar.cs

    // DoActivate.cs, builds DoActivate.exe
    using System;
    using System.Reflecti on;

    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    IFoo ifoo;
    Object obj;

    try
    {
    Assembly a = Assembly.LoadFr om( "Activate.d ll" );
    Type[] ts = a.GetTypes();
    foreach( Type t in ts )
    {
    if( t.Name == "FooBar" )
    {
    if( t.GetInterface( "IFoo" ) != null )
    {
    obj = Activator.Creat eInstance( t );
    ifoo = obj as IFoo;
    if( ifoo == null )
    Console.WriteLi ne("didn't get object" );
    else
    Console.WriteLi ne("got object" );
    }
    else
    Console.WriteLi ne( "Object does not support
    IFoo" );
    }
    }
    }
    catch( Exception e )
    {
    Console.WriteLi ne( "Caught exception {0}", e );
    }
    }
    }
    // End of DoActivate.cs

    Why is ifoo null? I always get "didn't get object" and yet everything
    appears correct, at least to my untrained eye. Thanks in advance!

  • Mattias Sjögren

    #2
    Re: Dynamically loading assemblies and interfaces

    Andy,

    Why are you using Assembly.LoadFr om on an assembly that you obviously
    have referenced at design time?

    [color=blue]
    >Why is ifoo null?[/color]






    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Andy Walldorff

      #3
      Re: Dynamically loading assemblies and interfaces

      Mattias Sjögren wrote:[color=blue]
      > Andy,
      >
      > Why are you using Assembly.LoadFr om on an assembly that you obviously
      > have referenced at design time?
      >
      >[/color]
      Ultimately I want to be able to dynamically load other implementations
      of IFoo. This is just a trivial example to display the problem I am
      having, not with LoadFrom (it seems to work fine), but with attempting
      to treat the returned object as an IFoo. Everything seems to say I
      should be able to do this (i.e. code samples, the fact that GetInterface
      says it's available, ...), yet when I actually try to cast the object
      to an IFoo, it fails.

      Interestingly enough, if I create the object via new, I am able to cast
      it without a problem.
      [color=blue]
      >[color=green]
      >>Why is ifoo null?[/color]
      >
      >
      > http://www.gotdotnet.com/team/clr/Lo...Isolation.aspx
      > http://blogs.gotdotnet.com/suzcook/P...7-0a144c8dbf16
      >
      >
      >
      > Mattias
      >[/color]

      Comment

      • Mattias Sjögren

        #4
        Re: Dynamically loading assemblies and interfaces

        Andy,
        [color=blue]
        >Ultimately I want to be able to dynamically load other implementations
        >of IFoo.[/color]

        In that case I suggest you move the IFoo definition to a separate
        assembly. That would probably get rid of the problem.

        [color=blue]
        >This is just a trivial example to display the problem I am
        >having, not with LoadFrom (it seems to work fine), but with attempting
        >to treat the returned object as an IFoo.[/color]

        If you actually read the pages I posted links to, you'd find out that
        LoadFrom affects the type identity.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org

        Please reply only to the newsgroup.

        Comment

        Working...