public void addSearchPath(string path) { //File f = new File(path); //if (f.exists() && f.isDirectory()) if (File.Exists(path) || Directory.Exists(path)) { try { path = Path.GetFullPath(path);//f.getCanonicalPath(); foreach (string prefix in searchPath) { if (prefix == path) { return; } } UI.printInfo(UI.Module.SYS, "Adding {0} search path: \"{1}\"", type, path); searchPath.AddLast(path); } catch (Exception e) { UI.printError(UI.Module.SYS, "Invalid {0} search path specification: \"{1}\" - {2}", type, path, e); } } else { UI.printError(UI.Module.SYS, "Invalid {0} search path specification: \"{1}\" - invalid directory", type, path); } }
/** * Define a new plugin type from an existing class. This checks to make sure * the provided class is default constructible (ie: has a constructor with * no parameters). If the plugin type name was previously associated with a * different class, it will be overriden. This allows the behavior core * classes to be modified at runtime. * * @param name plugin type name * @param pluginClass class object for the plugin class * @return <code>true</code> if the plugin registered successfully, * <code>false</code> otherwise */ public bool registerPlugin(string name, Type pluginClass) { // check that the given class is compatible with the base class if (pluginClass.GetConstructor(Type.EmptyTypes) == null) { UI.printError(UI.Module.API, "Plugin \"{0}\" could not be declared - default constructor was not found", name); return(false); } if (pluginClasses.ContainsKey(name)) { UI.printWarning(UI.Module.API, "Plugin \"{0}\" was already defined - overwriting previous definition", name); } pluginClasses.Add(name, pluginClass); return(true); }
/** * Create an object from the specified type name. If this type name is * unknown or invalid, <code>null</code> is returned. * * @param name plugin type name * @return an instance of the specified plugin type, or <code>null</code> * if not found or invalid */ public T createObject(string name) { Type c; if (name == null || name.Equals("none")) { return(default(T)); } if (pluginClasses.ContainsKey(name)) { c = (Type)pluginClasses [name]; } else { // don't print an error, this will be handled by the caller return(default(T)); } try { return((T)Activator.CreateInstance(c)); } catch (MissingMethodException e) { UI.printError(UI.Module.API, "Cannot create object of type \"{0}\" - {1}", name, e.Message); return(default(T)); } }