By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,338 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,338 IT Pros & Developers. It's quick & easy.

Dynamically loading assemblies and interfaces

P: n/a
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.WriteLine( "FooBar" );
}
}
// End of FooBar.cs

// DoActivate.cs, builds DoActivate.exe
using System;
using System.Reflection;

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.LoadFrom( "Activate.dll" );
Type[] ts = a.GetTypes();
foreach( Type t in ts )
{
if( t.Name == "FooBar" )
{
if( t.GetInterface( "IFoo" ) != null )
{
obj = Activator.CreateInstance( t );
ifoo = obj as IFoo;
if( ifoo == null )
Console.WriteLine("didn't get object" );
else
Console.WriteLine("got object" );
}
else
Console.WriteLine( "Object does not support
IFoo" );
}
}
}
catch( Exception e )
{
Console.WriteLine( "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!

Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Andy,

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

Why is ifoo null?


http://www.gotdotnet.com/team/clr/Lo...Isolation.aspx
http://blogs.gotdotnet.com/suzcook/P...7-0a144c8dbf16

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 13 '05 #2

P: n/a
Mattias Sjögren wrote:
Andy,

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

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.
Why is ifoo null?

http://www.gotdotnet.com/team/clr/Lo...Isolation.aspx
http://blogs.gotdotnet.com/suzcook/P...7-0a144c8dbf16

Mattias


Nov 13 '05 #3

P: n/a
Andy,
Ultimately I want to be able to dynamically load other implementations
of IFoo.
In that case I suggest you move the IFoo definition to a separate
assembly. That would probably get rid of the problem.

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.


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
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.