473,473 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problems with CreateInstance after dynamically loading Assembly

I am trying to load an assembly and execute a method from a class.
I have listed 3 parts to the code.
1) The Driver to load and execute
2) The Interface
3) The assembly

I have a valid referece to the assembly. When I try to create an instance
of the class, it returns a null value.

Any help would be appreciated.

As a side note, if someone created their own IPlugIn and was wanting to use
it in my application, Is there a way to acertain their class which is equivalent
to my MainEntry? At the time that they create their assembly, they wouldn't
necessarily know to name their class MainEntry.

Thanks,
Dave
Da**********@BellSouth.net
================================================== =
IPlugIn plugin = null;
Assembly assembly = null;

assembly = Assembly.LoadFrom(assemblyName);
plugin = (IPlugIn)assembly.CreateInstance("IPlugIn.MainEntr y");

================================================== =

namespace Plugin
{
/// <summary>
/// Summary description for IPlugIn.
/// </summary>
public interface IPlugIn
{
string PluginName{get;}
void ExecutePlugin();
}
}
================================================== =
public class MainEntry : IPlugIn
{
private int m_pluginID = 1;
private string m_pluginName = "Plugin #1";
public string PluginName {get {return m_pluginName;}}
public int PluginID {get {return m_pluginID;} set {m_pluginID = value;}}

public void ExecutePlugin()
{
MessageBox.Show("ExecutePlugin has executed", "ExecutePlugin()");
}
}

================================================== =

Jul 19 '05 #1
4 11661
David Elliott <Da**********@BellSouth.net> wrote:
I am trying to load an assembly and execute a method from a class.
I have listed 3 parts to the code.
1) The Driver to load and execute
2) The Interface
3) The assembly

I have a valid referece to the assembly. When I try to create an instance
of the class, it returns a null value.

Any help would be appreciated.
You're using the wrong classname - in the code you've posted, the
classname is just MainEntry, but you're asking for IPlugIn.MainEntry.
As a side note, if someone created their own IPlugIn and was wanting to use
it in my application, Is there a way to acertain their class which is
equivalent to my MainEntry? At the time that they create their assembly,
they wouldn't necessarily know to name their class MainEntry.


What do you mean by "equivalent to"? You *can* tell whether or not a
type implements an interface, presumably IPlugIn in this case - that's
usually the best way to go.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #2
I tried again with the change from IPlugIn.MainEntry to MainEntry. After executing the
plugin = (IPlugIn)assembly.CreateInstance("MainEntry");
the result was plugin = null.

As far as the second question. If I name my class MainEntry and you named yours MyEntry
how would I know in the application. The difference would be
assembly.CreateInstance("MainEntry")
assembly.CreateInstance("MyEntry")

Thanks,
Dave

On Sat, 9 Aug 2003 08:23:36 +0100, Jon Skeet <sk***@pobox.com> wrote:
David Elliott <Da**********@BellSouth.net> wrote:
I am trying to load an assembly and execute a method from a class.
I have listed 3 parts to the code.
1) The Driver to load and execute
2) The Interface
3) The assembly

I have a valid referece to the assembly. When I try to create an instance
of the class, it returns a null value.

Any help would be appreciated.


You're using the wrong classname - in the code you've posted, the
classname is just MainEntry, but you're asking for IPlugIn.MainEntry.
As a side note, if someone created their own IPlugIn and was wanting to use
it in my application, Is there a way to acertain their class which is
equivalent to my MainEntry? At the time that they create their assembly,
they wouldn't necessarily know to name their class MainEntry.


What do you mean by "equivalent to"? You *can* tell whether or not a
type implements an interface, presumably IPlugIn in this case - that's
usually the best way to go.

Jul 19 '05 #3
David Elliott <Da**********@BellSouth.net> wrote:
I tried again with the change from IPlugIn.MainEntry to MainEntry.
After executing the
plugin = (IPlugIn)assembly.CreateInstance("MainEntry");
the result was plugin = null.
Could you post a short but complete program that demonstrates this
then? (It's easiest to make it a console app rather than a GUI one.)

Here's a short but complete program based on your code which *does*
work:

Test.cs:
using System;
using System.Reflection;
using Plugin;

public class Test
{
static void Main()
{
IPlugIn plugin = null;
Assembly assembly = null;

assembly = Assembly.LoadFrom("mainentry.dll");
plugin = (IPlugIn)assembly.CreateInstance("MainEntry");
Console.WriteLine (plugin);
plugin.ExecutePlugin();
}
}

IPlugin.cs:
namespace Plugin
{
/// <summary>
/// Summary description for IPlugIn.
/// </summary>
public interface IPlugIn
{
string PluginName{get;}
void ExecutePlugin();
}
}

MainEntry.cs:
using Plugin;
using System;

public class MainEntry : IPlugIn
{
public string PluginName {get {return "hello";}}

public void ExecutePlugin()
{
Console.WriteLine("ExecutePlugin has executed");
}
}

Compiling:
csc /target:library /out:iplugin.dll IPlugin.cs
csc /r:iplugin.dll Test.cs
csc /target:library /out:mainentry.dll /r:iplugin.dll MainEntry.cs

Output:
MainEntry
ExecutePlugin has executed
As far as the second question. If I name my class MainEntry and
you named yours MyEntry how would I know in the application.
The difference would be
assembly.CreateInstance("MainEntry")
assembly.CreateInstance("MyEntry")


You would use assembly.GetTypes and check each type in the assembly to
see whether or not it implemented the interface.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #4
I started to do this before you asked for a new project. Cut and pasted code and poof
everything magically worked. I copied the source in my original project, deleted the file,
created a new one with the same code and works now.

Don't know what happened.

Thanks for all your help.
Dave

On Sun, 10 Aug 2003 07:46:57 +0100, Jon Skeet <sk***@pobox.com> wrote:
David Elliott <Da**********@BellSouth.net> wrote:
I tried again with the change from IPlugIn.MainEntry to MainEntry.
After executing the
plugin = (IPlugIn)assembly.CreateInstance("MainEntry");
the result was plugin = null.


Could you post a short but complete program that demonstrates this
then? (It's easiest to make it a console app rather than a GUI one.)

Here's a short but complete program based on your code which *does*
work:

Test.cs:
using System;
using System.Reflection;
using Plugin;

public class Test
{
static void Main()
{
IPlugIn plugin = null;
Assembly assembly = null;

assembly = Assembly.LoadFrom("mainentry.dll");
plugin = (IPlugIn)assembly.CreateInstance("MainEntry");
Console.WriteLine (plugin);
plugin.ExecutePlugin();
}
}

IPlugin.cs:
namespace Plugin
{
/// <summary>
/// Summary description for IPlugIn.
/// </summary>
public interface IPlugIn
{
string PluginName{get;}
void ExecutePlugin();
}
}

MainEntry.cs:
using Plugin;
using System;

public class MainEntry : IPlugIn
{
public string PluginName {get {return "hello";}}

public void ExecutePlugin()
{
Console.WriteLine("ExecutePlugin has executed");
}
}

Compiling:
csc /target:library /out:iplugin.dll IPlugin.cs
csc /r:iplugin.dll Test.cs
csc /target:library /out:mainentry.dll /r:iplugin.dll MainEntry.cs

Output:
MainEntry
ExecutePlugin has executed
As far as the second question. If I name my class MainEntry and
you named yours MyEntry how would I know in the application.
The difference would be
assembly.CreateInstance("MainEntry")
assembly.CreateInstance("MyEntry")


You would use assembly.GetTypes and check each type in the assembly to
see whether or not it implemented the interface.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon X-Session | last post by:
Hi ! I hope somebody can help me, i'm completly out of mind. My problem: I have a class inherited from Windows.Forms.Control named BasicModule, which implements an interface named IModule...
0
by: Stephane Belzile | last post by:
Hi, I have been trying for more than 3 days to load an assembly and execute a method from a specific class. Loading the assembly from the GAC using it's strong name works fine but when I try to...
3
by: Daylor | last post by:
hi. im loading assembly from my computer and createInstance from it. till now it worked ok. now, im changing the path from : c:\myfolder\myassembly.dll to
1
by: John Jenkins | last post by:
Hi, I have a fairly simeple question. What are the differences between Assembly.CreateInstance and System.Activator.CreateInstance? I had read that one maps to the other, however when I use...
2
by: Razzie | last post by:
Hey all, I'm working on this project where I'm dynamically loading an assembly. Basically what I want is that I can just replace my old dll file with a new one without having to do anything...
3
by: System.Reflection Activator | last post by:
************************************** //Load the Assembly Assembly a = Assembly.LoadFrom(sAssembly); //Get Types so we can Identify the Interface. Type mytypes = a.GetTypes(); BindingFlags...
4
by: David Elliott | last post by:
I am trying to load an assembly and execute a method from a class. I have listed 3 parts to the code. 1) The Driver to load and execute 2) The Interface 3) The assembly I have a valid referece...
5
by: ismail.mayat | last post by:
Hello, I am having a problem with Activator.CreateInstance. I have the following code Assembly asm = System.Reflection.Assembly.LoadFrom(assembly); if(asm!=null) { try {
0
by: xievvv | last post by:
I'm working on a COM add-in project for Office. Because I don't want the overheard of VSTO, I'm going with the approach of using a C++ shim, as created by the wizard provided by MS for generating...
0
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...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.