CreateInstance problem

G

Guest

Hi all

I have read the posting of Chris Bamford, but I still get the problem...

Here are some pieces of code

file IPlugin.cs
public interface IPlugi

string SayHi()

file Class1.c
public class Class1: IPlugi

string IPlugin.SayHi(

return "Hi"



These excerpts are compiled into test.dll

file form1.c
Assembly cAssembly = Assembly.LoadFrom(@"C:\test.dll")
foreach(Type cType in cAssembly.GetTypes()

if(cType.IsClass

if(cType.GetInterface("IPlugin") != null

test.IPlugin Plugin = (test.IPlugin)Activator.CreateInstance(cType)
textBox1.Text = Plugin.SayHi()




Can anybody tell me why I keep getting a System.InvalidCastException in

test.IPlugin Plugin = (test.IPlugin)Activator.CreateInstance(cType)
??

Thanx in advance
Hans
 
G

Guest

The code is based o

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/pluginframework.as

The <snip> can be found in step

public object Create(object parent,
object configContext,
System.Xml.XmlNode section

//Derived from CollectionBas
PluginCollection plugins = new PluginCollection()
foreach(XmlNode node in section.ChildNodes

tr

//Use the Activator class's 'CreateInstance' metho
//to try and create an instance of the plugin b
//passing in the type name specified in the attribute valu
object plugObject =
Activator.CreateInstance(Type.GetType(node.Attributes["type"].Value))

//Cast this to an IPlugin interface and add to the collectio
IPlugin plugin = (IPlugin)plugObject
plugins.Add(plugin)

catch(Exception e

//Catch any exception
//but continue iterating for more plugin


return plugins
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top