473,809 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!

Nov 13 '05 #1
3 10883
Andy,

Why are you using Assembly.LoadFr om 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
Mattias Sjögren wrote:
Andy,

Why are you using Assembly.LoadFr om 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2997
by: lanky_tx | last post by:
Hi All, We have an automated build and test environment using NAnt and Nunit. Some of our assemblies are being strong named by modifying the AssemblyInfo.cs and having csc compile it. Some of these strong named assemblies are being dynamically loaded into the runtime. We store the metadata information about the strong named assemblies in a config file. Metadata in the config file looks like this:
0
1329
by: Verane | last post by:
Hi all, I am working with C# and Visual studio 2003. What I want to do is the following : I have 3 assemblies, let call them A.exe, B.dll and C.dll. I want to dynamically load B and C when A is loaded. B and C are statically referencing A. Those three projects are in the same solution called mySolution.
7
3045
by: Ollie Riches | last post by:
I am trying to dynamically load an assembly that has a reference to 'Interop.WMEncoderLib.dll' which is a PIA to the windows media player DRM components. When I run the code from a console application it works perfectly fine, but when the assembly containing the reference is load from a windows service an exception is thrown with the following message: "File or assembly name Interop.WMEncoderLib, or one of its dependencies, was not...
2
1268
by: Ravi | last post by:
We have an application which dynamically loads an assembly and creates instances of classes. For this we have written a factory class which reflects the assembly and creates the classes and returns the created objects as plain objects. The caller method casts the object returned by the object factory into appropriate class object and uses it. All this works fine when exe is invoked directly. But when the application is deployed on the...
6
6879
by: Dan Dorey | last post by:
I actually have two questions here, but I'll start by giving an outline of what I'm trying to do. I'm building an app with a simple plugin architecture (all in the same app domain). I have each plugin in a separate sub-dir inside a "Plugins" directory. Each plugin consists of at least two DLLs (a controller and a driver). However, most drivers also rely on other external DLLs. In one particular case this dependency is to a C++ DLL...
2
3121
by: Smithers | last post by:
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
2
5117
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application = Windows Forms class B = a singleton hosted within A. B is responsible for dynamically loading classes X, Y, and Z.
1
2242
by: =?Windows-1252?Q?Tor_B=E5dshaug?= | last post by:
BlankHi, I am having trouble loading assemblies from the database in my ASP.NET app. I have a default.aspx in my app that is served from a database via a custom virtual path provider. This works fine, until this default.aspx uses code in a dependent assembly (say CustomAssembly). Then ASP.NET cannot find the class "CustomAssembly.MyClass" and files to compile the default.aspx. I've tried to do a AppDomain.CurrentDomain.Load(byte...
3
2032
by: Matthias S. | last post by:
Hi there, i'm planning on creating a Windows Service, which will have a simple loop in its OnStart override. In this loop, I dynamically want to load (job) Assemblies which have a single class implementing a JobInterface (just an Execute() method). The Service should then execute this method in a new thread. These Execute() methods won't take more then a couple of seconds to do what they have to. At the end of the loop in OnStart, there...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.