static void Main(string[] args) { //インストールされているプラグインを調べる PluginInfo[] pis = Plugin.PluginInfo.FindPlugins(); //すべてのプラグインクラスのインスタンスを作成する Plugin.IPlugin[] plugins = new Plugin.IPlugin[pis.Length]; for (int i = 0; i < plugins.Length; i++) { plugins[i] = pis[i].CreateInstance(); } //有効なプラグインを表示し、使用するプラグインを選ばせる int number = -1; do { for (int i = 0; i < plugins.Length; i++) { Console.WriteLine("{0}:{1}", i, plugins[i].Name); } Console.WriteLine( "使用するプラグインの番号を入力してください。:"); try { number = int.Parse(Console.ReadLine()); } catch { Console.WriteLine("数字を入力してください。"); } } while (number < 0 || number >= pis.Length); Console.WriteLine(plugins[number].Name + " を使用します。"); //プラグインに渡す文字列の入力を促す Console.WriteLine("文字列を入力してください。:"); string str = Console.ReadLine(); //プラグインのRunメソッドを呼び出して結果を取得する string result = plugins[number].Run(str); //結果の表示 Console.WriteLine("結果:"); Console.WriteLine(result); Console.ReadLine(); }
/// <summary> /// Loads a plugin /// </summary> /// <param name="FullName">Full path to a plugin's file</param> /// <returns>"true" after successful loading, else "false"</returns> public bool LoadPlugin(string FullName) { Assembly pluginAssembly; try { pluginAssembly = Assembly.LoadFile(FullName); } catch (Exception ex) // Can't load the assembly's file { SharedData.Logger.Append(ex.Message, 1); return(false); } try { Type t = pluginAssembly.GetTypes().FirstOrDefault(x => x.BaseType == typeof(Plugin.IPlugin)); Plugin.IPlugin plugin = (Plugin.IPlugin)Activator.CreateInstance(t); Plugins.Add(pluginAssembly, plugin); plugin.Host = this; SharedData.Logger.Append(String.Format("Plugin '{0}' has been loaded successfully.", plugin.Name, plugin.Description), 0); try { plugin.Configuration = (Plugin.ConfigurationBase) typeof(Plugin.ConfigurationBase) .GetMethod("Open").MakeGenericMethod(pluginAssembly.GetTypes() .FirstOrDefault(x => x.BaseType == typeof(Plugin.ConfigurationBase))) .Invoke(null, new object[] { System.IO.Path.GetFileNameWithoutExtension(FullName), Assembly.GetCallingAssembly().Location }); } // There is not a configuration section's class in the plugin or an error occured during its loading. catch (Exception ex) { SharedData.Logger.Append(String.Format("Can't load configuration for the Plugin {0}: {1}", pluginAssembly.GetName().Name, ex.Message), 1); // Working with default configuration } return(true); } catch (Exception ex) { SharedData.Logger.Append(ex.Message, 1); return(false); } }