{"id":228,"date":"2010-01-12T09:53:12","date_gmt":"2010-01-12T09:53:12","guid":{"rendered":"http:\/\/dirk.forbiddendream.be\/?p=228"},"modified":"2016-01-07T10:50:18","modified_gmt":"2016-01-07T10:50:18","slug":"creating-plugins-in-c-the-handler-part-24","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=228","title":{"rendered":"Creating plugins in C#: The handler (Part 2\/4)"},"content":{"rendered":"<p>Welcome to the second part of our little tutorial ^.^<\/p>\n<p>In this part I&#8217;ll explain how the <strong>handler<\/strong> works and what you need in order for it to work&#8230;<\/p>\n<p><!--more--><\/p>\n<h1>The handler<\/h1>\n<p>The handler consists out of 2 things:<\/p>\n<ul>\n<li>An interface<\/li>\n<li>A class that extends the Attribute class<\/li>\n<\/ul>\n<p>Add a new\u00a0<strong>C# Class Library project<\/strong> and name it\u00a0<strong>PluginHandler<\/strong>.<\/p>\n<p><span style=\"color: #ff0000;\"><span style=\"text-decoration: underline;\">Delete<\/span><\/span> the default class that gets created (<em>Class1.cs<\/em>)<\/p>\n<h2>The interface<\/h2>\n<p>Add a new\u00a0<strong>Interface<\/strong> named\u00a0<strong>IPlugin<\/strong> (<em>Add &gt; New Item &gt; Interface<\/em>)<\/p>\n<figure id=\"attachment_214\" aria-describedby=\"caption-attachment-214\" style=\"width: 373px\" class=\"wp-caption alignnone\"><a rel=\"attachment wp-att-214\" href=\"http:\/\/dirk.schuermans.me\/?attachment_id=214\"><img loading=\"lazy\" class=\"size-full wp-image-214   \" title=\"add_interface\" src=\"http:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_interface.png\" alt=\"\" width=\"373\" height=\"222\" srcset=\"https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_interface.png 799w, https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_interface-300x178.png 300w\" sizes=\"(max-width: 373px) 100vw, 373px\" \/><\/a><figcaption id=\"caption-attachment-214\" class=\"wp-caption-text\">Adding a new interface<\/figcaption><\/figure>\n<p>The reason we include the interface, is because any plugin you make must implement the interface and needs it in order to be shown.<br \/>\nSince this is just a basic explanation of how it works, I&#8217;m only going to show you what you need in order to load your plugin.<\/p>\n<p>Interface source code:<\/p>\n<pre name=\"code\" class=\"c-sharp\">\/\/ Plugin interface for the plugin handler\r\nusing System;\r\n\r\nnamespace PluginHandler\r\n{\r\n    public interface IPlugin\r\n    {\r\n        void ShowPlugin();\r\n    }\r\n}<\/pre>\n<h2>The class<\/h2>\n<p>Now that we have our interface, we add a new <strong>class<\/strong> called <strong>PluginAttribute<\/strong>: (<em>Add &gt; New Item &gt; Class<\/em>)<\/p>\n<figure id=\"attachment_219\" aria-describedby=\"caption-attachment-219\" style=\"width: 373px\" class=\"wp-caption alignnone\"><a rel=\"attachment wp-att-219\" href=\"http:\/\/dirk.schuermans.me\/?attachment_id=219\"><img loading=\"lazy\" class=\"size-full wp-image-219   \" title=\"add_pluginattribute\" src=\"http:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_pluginattribute.png\" alt=\"\" width=\"373\" height=\"222\" srcset=\"https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_pluginattribute.png 798w, https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_pluginattribute-300x178.png 300w\" sizes=\"(max-width: 373px) 100vw, 373px\" \/><\/a><figcaption id=\"caption-attachment-219\" class=\"wp-caption-text\">Adding a new class<\/figcaption><\/figure>\n<p>Class source code:<\/p>\n<pre name=\"code\" class=\"c-sharp\">\/\/ Plugin class that extends the attribute class\r\nusing System;\r\n\r\nnamespace PluginHandler\r\n{\r\n    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]\r\n    public sealed class PluginAttribute : Attribute { }\r\n}<\/pre>\n<h2>Build<\/h2>\n<p>Now that we have added the 2 objects and modified their code, it&#8217;s time to build it.<\/p>\n<p><em>Build &gt; Build PluginHandler<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p><strong><a href=\"http:\/\/dirk.schuermans.me\/?p=240\" target=\"_self\">Continue to the next part of this tutorial&#8230;<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the second part of our little tutorial ^.^ In this part I&#8217;ll explain how the handler 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\/228"}],"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=228"}],"version-history":[{"count":4,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":546,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions\/546"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}