Looks fine to me. In fact, you did use the Activator class:
mIpi[i] = (IPlugin)Activator.CreateInstance(ObjType);
: : Take a look at the System.Activator class. With that, you can dynamically instantiate classes of whatever assembly, from whatever assembly.
: :
: : Greets,
: : Eric Goldstein
: : www.gvh-maatwerk.nl
: :
: :
: Thanks Eric,
:
: the biggest problem for me is: I only have a couple of years experience,
: and some concepts of C# I only know from reading, but have never used.
: Then there is this huge project I have to change and rid of bugs.
:
: Now what I described in my question I have solved meanwhile. However, have a look at this code snippet, I wonder if this is the propper way to implement the objects of my plugins?
:
: string path = Application.StartupPath + "\\Standard\\PlugIn";
: string[] pluginFiles = Directory.GetFiles(path, "*.dll");
: // List of Interface - Objects
: this.mIpi = new IPlugin[pluginFiles.Length];
:
: // register ViskonInterface foreach Dll
:
: for(int i=0; i<pluginFiles.Length; i++)
: {
: string args = pluginFiles[i].Substring( pluginFiles[i].LastIndexOf("\\")+1,
: pluginFiles[i].IndexOf(".dll")-
: pluginFiles[i].LastIndexOf("\\")-1);
:
:
: Type ObjType = null;
: try
: {
: Assembly ass = null;
: ass = Assembly.LoadFile( pluginFiles[i] );
:
: Type[] mytypes = ass.GetTypes();
:
: if (ass != null)
: {
: ObjType = ass.GetType(args+".Plugin");
: }
: }
: catch (Exception ex)
: {
: MessageBox.Show(ex.ToString(),"Exception Read Dll");
: }
:
: try
: {
: if (ObjType != null)
: //only if Dll class is called Plugin!!
: {
: mIpi[i] = (IPlugin)Activator.CreateInstance(ObjType);
: // catch if plugIn not derived of type VisKonInterface
: mIpi[i].PlugInHost = this;
: mIpi[i].Initialize();
: }
: }
: ....................................
: ..................................
:
: Does that make sense?!
:
: cya carsten
:
Greets,
Eric Goldstein
www.gvh-maatwerk.nl