Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I type safely load code from multiple sources that deal with an unknown type?
    primarykey
    data
    text
    <p>I need to be able to, at runtime, load two classes that share an unknown type, like this:</p> <pre><code>interface IProducer&lt;out A&gt; { public A CookOne(); } interface IConsumer&lt;in A&gt; { public void EatOne(A food); } </code></pre> <h2>&lt;edit&gt;</h2> <p>Here are two example classes</p> <pre><code>class IntChef : IProducer&lt;int&gt; { private int _counter = 0; public int CookOne() { return counter++; } } class IntCustomer : IConsumer&lt;int&gt; { public void EatOne(int food) { Console.WriteLine("I ate a {0}", food); } } </code></pre> <p>The goal is to load an implementation of each interface, sharing the type they communicate via, and create a new object that doesn't care how the two loaded types communicate.</p> <h2>&lt;/edit&gt;</h2> <p>The entire list of possible involved types can't be known at compile time because they don't exist yet. The software that uses these types is something like this:</p> <pre><code>interface IStrategyFactory&lt;A&gt; { public IStrategy MakeAStrategy(IProducer&lt;A&gt; producer, IConsumer&lt;A&gt; consumer); } interface IStrategy { public void DoSomething();; } </code></pre> <p>How can I load external binary code (from two different sources) and pass them to the factory (or wherever else) in a type-safe manner, so that I can't, for example, pass in a IProducer and an IConsumer?</p> <p>The best resource I can find on this so far is <a href="http://codebetter.com/blogs/glenn.block/archive/2009/08/20/open-generic-support-in-mef.aspx" rel="nofollow noreferrer">http://codebetter.com/blogs/glenn.block/archive/2009/08/20/open-generic-support-in-mef.aspx</a></p> <p>Optimally the program itself would have types like this:</p> <pre><code>interface IStrategyFactory&lt;A&gt; { public IStrategy MakeAStrategy(IProducer&lt;A&gt; producer, IConsumer&lt;A&gt; consumer); } interface IStrategy { public void DoSomething();; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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