Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically loading assembly: why this code works?
    primarykey
    data
    text
    <p>In my situation there are three components: <code>Consumer</code> class, <code>IExposedIface</code> interface and <code>Exposed</code> class implementing <code>IExposedIface</code>. Both <code>Consumer</code> and <code>Exposed</code> are linked statically with <code>IExposedIface</code>, but <code>Consumer</code> bears no compile-time reference to <code>Exposed</code>.</p> <p>I am trying to come up with a scheme which would allow <code>Consumer</code> loading different versions of <code>Exposed</code> at runtime (depending on input data - let's say each input document carries an information about which version of <code>Exposed</code> should be used to process it). To achieve this, I started studying <strong>AppDomains</strong> and now I have a basic version working.</p> <p>So far it seems to me there are two options when it comes to providing <code>IExposedIface</code> assembly to <code>Exposed</code> assembly.</p> <ol> <li><p>Having <code>IExposedIface.dll</code> only in <code>Consumer</code>'s <strong>bin</strong> directory and handling <code>AppDomain.AssemblyResolve</code> event for the <code>AppDomain</code> in which I am creating an instance of <code>Exposed</code></p></li> <li><p>Having <code>IExposedIface.dll</code> both in <code>Consumer</code>'s <strong>bin</strong> directory as well as aside each <code>Exposed.dll</code>.</p></li> </ol> <p>Now consider that I build <code>Exposed</code> against this <code>IExposedIface</code>:</p> <pre><code>public interface IExposedIface { string SaySomething(); } </code></pre> <p>and I build <code>Consumer</code> against this <code>IExposedIface</code>:</p> <pre><code>public interface IExposedIface { string SaySomething(); string SaySomethingDifferent(); } </code></pre> <p>In the first case, the exception</p> <blockquote> <p>Exception: <strong>Method 'SaySomethingDifferent'</strong> in type 'Exposed.Exposed' from assembly 'Exposed, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' <strong>does not have an implementation</strong>.</p> </blockquote> <p>is thrown in the moment I call <code>appDomain.CreateInstanceAndUnwrap(...)</code> to create an instance of <code>Exposed</code> in the freshly created <strong>AppDomain</strong>.</p> <p>That looks reasonable to me.</p> <p>But in the second case, <code>appDomain.CreateInstanceAndUnwrap(...)</code> goes through just fine and I can without problems call 'SaySomething()' method on the retrieved object. An exception</p> <blockquote> <p><strong>The method 'SaySomethingDifferent' was not found on the interface/type</strong> 'IExposedIface.IExposedIface, IExposedIface, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null'.</p> </blockquote> <p>is only thrown when I actually call <code>SaySomethingDifferent()</code> in <code>Consumer</code>.</p> <p>I was quite surprised that in this second case CLR had let me go so far...Could someone explain why this is possible?</p>
    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. 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