Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to decouple the two DLLs, placing the interfaces and implementation in two different DLLs, and then using late binding to instantiate the class.</p> <hr> <pre><code>// IFoo.cs: (build IFoo.dll) interface IFoo { void foo(int i); } public class FooFactory { public static IFoo CreateInstance() { return (IFoo)Activator.CreateInstance("Foo", "foo").Unwrap(); } } </code></pre> <hr> <pre><code>// IBar.cs: (build IBar.dll) interface IBar { void bar(int i); } public class BarFactory { public static IBar CreateInstance() { return (IBar)Activator.CreateInstance("Bar", "bar").Unwrap(); } } </code></pre> <hr> <pre><code>// foo.cs: (build Foo.dll, references IFoo.dll and IBar.dll) public class Foo : IFoo { void foo(int i) { IBar objBar = BarFactory.CreateInstance(); if (i &gt; 0) objBar.bar(i -i); } } </code></pre> <hr> <pre><code>// bar.cs: (build Bar.dll, references IBar.dll and IFoo.dll) public class Bar : IBar { void bar(int i) { IFoo objFoo = FooFactory.CreateInstance(); if (i &gt; 0) objFoo.foo(i -i); } } </code></pre> <p>The "Factory" classes are technically not necessary, but it's much nicer to say:</p> <pre><code>IFoo objFoo = FooFactory.CreateInstance(); </code></pre> <p>in application code than:</p> <pre><code>IFoo objFoo = (IFoo)Activator.CreateInstance("Foo", "foo").Unwrap(); </code></pre> <p>because of the following reasons:</p> <ol> <li>You can avoid a "cast" in application code, which is a good thing</li> <li>If the DLL that hosts the class changes, you don't have to change all the clients, just the factory.</li> <li>Code-completion still wroks.</li> <li>Depending on your needs, you have to call culture-aware or key-signed DLLs, in which case you can add more parameters to the CreateInstance call in the factory in one place.</li> </ol> <p>-- Kenneth Kasajian</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload