<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Plugins on ZeroSharp</title><link>https://www.zerosharp.com/blog/categories/plugins/</link><description>Recent content in Plugins on ZeroSharp</description><generator>Hugo</generator><language>en-gb</language><copyright>Copyright © 2012–{year} Robert Anderson</copyright><lastBuildDate>Wed, 17 Sep 2014 08:08:00 +0100</lastBuildDate><atom:link href="https://www.zerosharp.com/blog/categories/plugins/index.xml" rel="self" type="application/rss+xml"/><item><title>Replacing a class at runtime using Ninject and Roslyn - Part 4: Roslyn</title><link>https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-4/</link><pubDate>Wed, 17 Sep 2014 08:08:00 +0100</pubDate><guid>https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-4/</guid><description>&lt;p&gt;This is the fourth and final part of a series about using Roslyn with dependency injection to create a flexible and powerful plug-in framework. Here I review the parts of the solution that deal with the Roslyn runtime compilation of plug-ins. Check out &lt;a href="https://github.com/ZeroSharp/RoslynPlugins"&gt;the working example on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Previously&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-1/"&gt;Part 1: The Goal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-2/"&gt;Part 2: The Solution&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-3/"&gt;Part 3: Dependency Injection&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="roslyn"&gt;Roslyn&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s look at some of the main classes used to compile plug-in code at runtime.&lt;/p&gt;</description></item><item><title>Replacing a class at runtime using Ninject and Roslyn - Part 3: Dependency Injection</title><link>https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-3/</link><pubDate>Mon, 15 Sep 2014 08:01:00 +0100</pubDate><guid>https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-3/</guid><description>&lt;p&gt;This is the third part of a series about using Roslyn with dependency injection to create a flexible and powerful plug-in framework. Here I review the parts of the solution that deal with dependency injection. Check out &lt;a href="https://github.com/ZeroSharp/RoslynPlugins"&gt;the working example on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Previously&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-1/"&gt;Part 1: The Goal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-2/"&gt;Part 2: The Solution&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="dependency-injection"&gt;Dependency injection&lt;/h2&gt;
&lt;p&gt;The first trick is to use dependency injection to create any instance of the &lt;code&gt;HelloWorldGenerator&lt;/code&gt; class. Then if we need to add a new dependency to the class, we can just add it to the constructor without breaking anything.&lt;/p&gt;</description></item><item><title>Replacing a class at runtime using Ninject and Roslyn - Part 2: The Solution</title><link>https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-2/</link><pubDate>Thu, 11 Sep 2014 09:18:00 +0100</pubDate><guid>https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-2/</guid><description>&lt;p&gt;Previously: &lt;a href="https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-1/"&gt;Part 1: The Goal&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="the-solution"&gt;The solution&lt;/h2&gt;
&lt;p&gt;The code for the example is &lt;a href="https://github.com/ZeroSharp/RoslynPlugins"&gt;available on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id="how-it-looks"&gt;How it looks&lt;/h4&gt;
&lt;p&gt;So here&amp;rsquo;s the Hello World page in production:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://www.zerosharp.com/images/blog/roslyn-plugins-001.png" alt=""&gt;.&lt;/p&gt;
&lt;p&gt;We navigate to the plugins view and create a new replacement for the HelloWorldGenerator:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://www.zerosharp.com/images/blog/roslyn-plugins-002.png" alt=""&gt;.&lt;/p&gt;
&lt;p&gt;Without restarting, we can return to the HelloWorld page and see that the new class is being used because the output has changed.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://www.zerosharp.com/images/blog/roslyn-plugins-003.png" alt=""&gt;.&lt;/p&gt;
&lt;p&gt;If you delete the row from the plugins page, the behaviour reverts to the original implementation (the code that was originally shipped with production).&lt;/p&gt;</description></item><item><title>Replacing a class at runtime using Ninject and Roslyn - Part 1: The Goal</title><link>https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-1/</link><pubDate>Tue, 09 Sep 2014 10:47:00 +0100</pubDate><guid>https://www.zerosharp.com/replacing-a-class-at-runtime-using-ninject-and-roslyn-part-1/</guid><description>&lt;h2 id="the-goal"&gt;The goal&lt;/h2&gt;



How can we replace a given class&amp;rsquo;s code with new code at runtime? In particular, how we can we do this while allowing dependency injection and sidestepping assembly versioning issues.

&lt;p&gt;Let&amp;rsquo;s say you have bunch of classes like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c#" data-lang="c#"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeGenerator&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IGenerator&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;SomeGenerator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ISomeDependency&lt;/span&gt; &lt;span class="n"&gt;dependency&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IAnotherDependency&lt;/span&gt; &lt;span class="n"&gt;another&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Generate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// generate some output&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Now let&amp;rsquo;s assume that you need the ability to modify the behaviour of these classes at runtime without upgrading. And change the dependencies. Without restarting the application.&lt;/p&gt;</description></item></channel></rss>