Interface HELP!
I have:
1. Interface Library
2. Plugin Library
3. Plugin Host library which is responcisable for loading all of the plugins.
The Plug-in library loads the plugins is a Singelton and stores them in a .Collection class. This way all of the plugs-ins are available to the entire application.
To make a long story short i will only post the code that stores each Plug-In / Interface.
Here is the question-
Now that i have all of my plug-ins store away in my sweet little singelton ; and ready to server what ever caller. What is the best way to create another instance of the types of Plug-ins that are loaded? For instance; I have a tabPage that holds a plug-in.. This tabPage repersents ONE instance of a product view.. The client will create my Instances of the same Plug-In; however, i only have ONE instance of each plug-in in my Singelton.Collection.
Here is what i have done and please let me know if this way is ok... For performence reason.. I feel like this might be ok but i just want to make sure..
-Here is the object that holds the indivual Plug-ins
publicclassAvailablePlugin
{
private ISuperPlugin.ISuperPlugin _iPlugin =null;
privateType _theType =null;
public ISuperPlugin.ISuperPlugin Instance
{
get {return _iPlugin; }
set { _iPlugin =value; }
}
publicType TheType
{
get {return _theType; }
set { _theType =value; }
}
public ISuperPlugin.ISuperPlugin SendNewType()
{
ISuperPlugin.ISuperPlugin newPlugIn = (ISuperPlugin.ISuperPlugin)Activator.CreateInstance(TheType);
newPlugIn.Host =this.Instance.Host;
return newPlugIn;
}
publicoverridestring ToString()
{
return Instance.ThePluginType.ToString();
}
}
If you will notice i have tucked the Type in a Property so that i can create an instance of it later on.
-(Is this way ok to keep creating instances ?
public ISuperPlugin.ISuperPlugin SendNewType()
{
ISuperPlugin.ISuperPlugin newPlugIn = (ISuperPlugin.ISuperPlugin)Activator.CreateInstance(TheType);
newPlugIn.Host =this.Instance.Host;
return newPlugIn;
}
-(Is this way ok to keep creating instances ?
I will go ahead and post how i load the object
privatevoid AddPlugin(string pluginAssemblyFile)
{
Assembly pluginAssembly =Assembly.LoadFrom(pluginAssemblyFile);
foreach (Type pluginTypein pluginAssembly.GetTypes())
{
if (pluginType.IsPublic)
{
if (!pluginType.IsAbstract){
Type typeInterface = pluginType.GetInterface(Plugin.Default.PluginInterfaceBase,true);
if (typeInterface !=null)
{
AvailablePlugin newPlugin =newAvailablePlugin();
Type type = pluginAssembly.GetType(pluginType.ToString());
newPlugin.TheType = type;
newPlugin.Instance =
(ISuperPlugin.ISuperPlugin)Activator.CreateInstance(type);
newPlugin.Instance.Host =this;
//Add the new plugin to our collection here
this.AvailablePlugins.Add(newPlugin);
newPlugin =null;
}
typeInterface =null;/
}
}
}
pluginAssembly =null;
}
thanks for the help!
erik

