473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 11654
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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,...
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...
0
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
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...

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.