Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's a slightly confusing topic, so here's a step-by-step example - now using the names from Eric's answer to help keep things consistent. We're going to start off with one library (Alpha.dll) and build an application (Test.exe) which depends on Alpha. We're then going to a type which Test.exe depends on (Foo) into a different library (Bravo.dll) without recompiling Test.exe.</p> <ol> <li><p>Create the following files:</p> <p><strong>Foo.cs</strong></p> <pre><code>using System; public class Foo { public static void Report() { Console.WriteLine("Foo.Report"); } } </code></pre> <p><strong>Test.cs</strong></p> <pre><code>class Test { static void Main() { Foo.Report(); } } </code></pre></li> <li><p>Build Alpha.dll:</p> <pre><code>csc /target:library /out:Alpha.dll Foo.cs </code></pre></li> <li><p>Build Test.exe</p> <pre><code>csc /r:Alpha.dll Test.cs </code></pre></li> <li><p>Run Test.exe - you should get the obvious output</p></li> <li><p>Build Bravo.dll:</p> <pre><code>csc /target:library /out:Bravo.dll Foo.cs </code></pre></li> <li><p>Create a new file, <strong>Forwarding.cs</strong>:</p> <pre><code>using System.Runtime.CompilerServices; [assembly:TypeForwardedTo(typeof(Foo))] </code></pre></li> <li><p>Recompile Alpha.dll:</p> <pre><code>csc /r:Bravo.dll /target:library /out:Alpha.dll Forwarding.cs </code></pre> <p>Note how we're <em>not</em> including the code for Foo in Alpha any more.</p></li> <li><p>Run Test.exe - it will still work, despite the fact that Test.exe <em>asks</em> for a reference to Foo within Alpha.dll... the CLR simply redirects that to Bravo.dll.</p> <p>If you look in Test.exe, it will still refer to Alpha. If you look in Alpha.dll, you'll find that the code for the Foo type isn't there any more... it's only through type forwarding that it all hangs together.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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