Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to assign an interface to an object at runtime?
    primarykey
    data
    text
    <p>After reflecting upon all the great answers I received to my <a href="https://stackoverflow.com/questions/3389588/how-should-i-model-my-code-to-maximize-code-re-use-in-this-specific-situation">previous question about modeling my code to maximize code re-use</a>, I started wondering if it would be possible to assign the interface to an object at runtime instead of when it is coded.</p> <p>Take, for example, a simplified portion of code from <a href="https://stackoverflow.com/questions/3389588/how-should-i-model-my-code-to-maximize-code-re-use-in-this-specific-situation">my previous question</a>:</p> <pre><code>public class OutboxManager { private IVendorMessenger _VendorMessenger; //This is the default constructor, forcing the consumer to provide //the implementation of IVendorMessenger. public OutboxManager(IVendorMessenger messenger) { _VendorMessenger = messenger; } public void DistributeOutboxMessages() { VendorMessenger.SendMessageToVendor() _OutboxMgrDataProvider.MarkMessageAsProcessed(om) } } </code></pre> <p>Currently, if someone wants to consume this class, they have to code a class that implements <code>IVendorMessenger</code> and provide it during initialization as a parameter:</p> <pre><code> var MyOutboxManger = new OutboxManager(new MyVendorMessenger()) </code></pre> <p>What if, instead of hard-coding the interface parameter, it instead gets assinged at runtime? This way, I could compile a dll for a class that implements <code>IVendorMessenger</code>, drop it into the same folder that <code>OutboxManagerExecutable</code> exists, and it will wire everything up at run time. I suppose, using this logic, I could figure out a way to drop <em>multiple</em> implementations of <code>IVendorMessenger</code> in the same folder and design the executable to be smart enough to iterate through all appropriate dlls and consume them accordingly.</p> <p>Is this type of functionality possible in .NET 4?</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.
 

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