{"id":243,"date":"2010-01-12T09:53:25","date_gmt":"2010-01-12T09:53:25","guid":{"rendered":"http:\/\/dirk.forbiddendream.be\/?p=243"},"modified":"2016-01-07T10:50:18","modified_gmt":"2016-01-07T10:50:18","slug":"creating-plugins-in-c-the-application-part-44","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=243","title":{"rendered":"Creating plugins in C#: The Application (Part 4\/4)"},"content":{"rendered":"<p>Welcome to the fourth and final part of our little tutorial ^.^<\/p>\n<p>In this part I&#8217;ll explain how the <strong>application <\/strong>works and what you need in order for it to work&#8230;<\/p>\n<p><!--more--><\/p>\n<h1>The application<\/h1>\n<p>Alright, we have our handler and plugin. It&#8217;s time to load the plugin from within our application \ud83d\ude09<\/p>\n<p>Add a new <strong>C# Windows Form project<\/strong> called <strong>TestApplication <\/strong>(<em>Add &gt; New Project<\/em>)<\/p>\n<p>Add a reference to the <strong>PluginHandler <\/strong>project<\/p>\n<figure id=\"attachment_270\" aria-describedby=\"caption-attachment-270\" style=\"width: 283px\" class=\"wp-caption alignnone\"><a rel=\"attachment wp-att-270\" href=\"http:\/\/dirk.schuermans.me\/?attachment_id=270\"><img loading=\"lazy\" class=\"size-full wp-image-270 \" title=\"add_handlerreference\" src=\"http:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_handlerreference.png\" alt=\"\" width=\"283\" height=\"239\" srcset=\"https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_handlerreference.png 471w, https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_handlerreference-300x254.png 300w\" sizes=\"(max-width: 283px) 100vw, 283px\" \/><\/a><figcaption id=\"caption-attachment-270\" class=\"wp-caption-text\">Adding a reference to an existing project<\/figcaption><\/figure>\n<p>Open the code view of the application (<em>TestApplication.cs<\/em>) and add the following using statements:<\/p>\n<pre name=\"code\" class=\"c-sharp\">using System.Reflection;\r\nusing System.IO;<\/pre>\n<p>Drag a <strong>TextBox<\/strong> onto the form and call it <strong>txtPlugin<br \/>\n<span style=\"font-weight: normal;\">Drag a <\/span>Button<span style=\"font-weight: normal;\"> onto the form and call it <\/span>btnBrowse<br \/>\n<span style=\"font-weight: normal;\">Drag a <\/span>Button<span style=\"font-weight: normal;\"> onto the form and call it <\/span>btnLoad<br \/>\n<span style=\"font-weight: normal;\">Drag an <\/span>OpenFileDialog<span style=\"font-weight: normal;\"> onto the form and call it <\/span>ofdPlugin<\/strong><\/p>\n<figure id=\"attachment_283\" aria-describedby=\"caption-attachment-283\" style=\"width: 395px\" class=\"wp-caption alignnone\"><a rel=\"attachment wp-att-283\" href=\"http:\/\/dirk.schuermans.me\/?attachment_id=283\"><img loading=\"lazy\" class=\"size-full wp-image-283\" title=\"application_design\" src=\"http:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/application_design.png\" alt=\"\" width=\"395\" height=\"45\" srcset=\"https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/application_design.png 659w, https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/application_design-300x34.png 300w\" sizes=\"(max-width: 395px) 100vw, 395px\" \/><\/a><figcaption id=\"caption-attachment-283\" class=\"wp-caption-text\">This is how it could look like<\/figcaption><\/figure>\n<p>Double click the <strong>btnBrowse<\/strong> button and add the following code:<\/p>\n<pre name=\"code\" class=\"c-sharp\">private void btnBrowse_Click(object sender, EventArgs e)\r\n{\r\n        ofdPlugin.Filter = \"Plugin | *.dll\";\r\n        ofdPlugin.FileName = \"\";\r\n        ofdPlugin.CheckFileExists = true;\r\n        ofdPlugin.FileOk += new CancelEventHandler(ofdPlugin_FileOk);\r\n\r\n        ofdPlugin.ShowDialog();\r\n}<\/pre>\n<p>Add the following method aswel (<em>This is the ofdPlugin.FileOk event handler<\/em>)l:<\/p>\n<pre name=\"code\" class=\"c-sharp\">private void ofdPlugin_FileOk(object sender, CancelEventArgs e)\r\n{\r\n        if (e.Cancel == false)\r\n        {\r\n                txtPlugin.Text = ofdPlugin.FileName;\r\n                btnLoad.Enabled = true;\r\n        }\r\n}<\/pre>\n<p>The above code will show an open file dialog when you click the browse button, allowing you to select the plugin you wish to load.<br \/>\nWhen you&#8217;ve selected the DLL you wish to load and it&#8217;s a file that exists, it&#8217;ll show the full path to the file in the textbox and enable the load button.<\/p>\n<p>Now double click the <strong>btnLoad<\/strong> button and add the following code:<\/p>\n<pre name=\"code\" class=\"c-sharp\">private void btnLoad_Click(object sender, EventArgs e)\r\n{\r\n        this.LoadPlugin(txtPlugin.Text);\r\n}<\/pre>\n<p>Now, if you honestly thought that it would be th\u00e1t simple, you&#8217;re wrong ^.^<br \/>\nBefore all of that works, we&#8217;ll need some extra code that acutally deals with the whole plugin loading thing:<\/p>\n<pre name=\"code\" class=\"c-sharp\">\/\/ Code snippet for loading the plugin\r\n        private void LoadPlugin(string p_sPlugin)\r\n        {\r\n            try\r\n            {\r\n                FileInfo fiPlugin = new FileInfo(p_sPlugin);\r\n                Assembly assembly = null;\r\n                string typeName = string.Empty;\r\n                Type pluginType = null;\r\n\r\n                assembly = Assembly.LoadFile(fiPlugin.FullName);\r\n\r\n                if (null != assembly)\r\n                {\r\n                    foreach (Type type in assembly.GetTypes())\r\n                    {\r\n                        if (type.IsAbstract) continue;\r\n                        if (type.IsDefined(typeof(PluginAttribute), true))\r\n                        {\r\n                            pluginType = type;\r\n                            break;\r\n                        }\r\n                    }\r\n\r\n                    if (null != pluginType)\r\n                    {\r\n                        ((IPlugin)Activator.CreateInstance(pluginType)).ShowPlugin();\r\n                    }\r\n                    else\r\n                    {\r\n                        this.ShowError(fiPlugin.Name + \" is not a valid plugin.\" + Environment.NewLine + Environment.NewLine + \"(Invalid plugin type)\");\r\n                    }\r\n                }\r\n\r\n            }\r\n            catch (System.InvalidCastException)\r\n            {\r\n                this.ShowError(fiPlugin.Name + \" is not a valid plugin.\" + Environment.NewLine + Environment.NewLine + \"(Missing interface)\");\r\n            }\r\n            catch (System.BadImageFormatException)\r\n            {\r\n                this.ShowError(fiPlugin.Name + \" is not a valid plugin.\" + Environment.NewLine + Environment.NewLine + \"(Invalid .NET Assembly)\");\r\n            }\r\n            catch (System.Exception ex)\r\n            {\r\n                this.ShowError(\"There was a problem loading \" + fiPlugin.Name);\r\n            }\r\n        }<\/pre>\n<p>The above method will do the following things:<\/p>\n<p>It will first check if we managed to load the assembly information of the DLL file.<br \/>\nAfter that it goes over each Type from the DLL and checks if it is an abstract type and if the type equals our PluginAttribute.<\/p>\n<p>When thats the case it will load the Plugin using the ShowPlugin() method \ud83d\ude42<\/p>\n<p>There we go, that&#8217;s it \ud83d\ude42<br \/>\nRun the application, select the plugin we created and load it! (<em>Make sure you hit <span style=\"text-decoration: underline;\">ctrl + shift + b<\/span> first so that it builds our entire solution<\/em>)<\/p>\n<p>If you have encountered any errors during this tutorial, please let me know so I can correct them \ud83d\ude09<br \/>\nBut as far as I know there shouldn&#8217;t be any \ud83d\ude00<\/p>\n<p>Enjoy!<\/p>\n<p>&#8211; Dirk<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the fourth and final part of our little tutorial ^.^ In this part I&#8217;ll explain how the application works and what you need in order for it to work&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[38,6],"tags":[],"_links":{"self":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/243"}],"collection":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=243"}],"version-history":[{"count":9,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":539,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/243\/revisions\/539"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}