Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>2 will be a performance killer (the necessary dynamic/relection <strong>invoke</strong> in particular is slow)</p> <p>1 is a common answer to this problem, especially with explicit implementation; the problem is getting hold of the type... does unity allow query with a <code>Type</code> instance, rather than via generics? If so something like below... of course, you might still have to use <code>MakeGenericType</code>:</p> <pre><code>Type intType = typeof(IProcessor&lt;&gt;).MakeGenericType(instanceType); IProcessor proc = (IProcessor) IoC.Resolve(intType); </code></pre> <p>Where <code>instanceType</code> is perhaps via <code>instance.GetType()</code>. For similar reasons, it might be helpful to expose the <code>T</code> as a <code>Type</code> on the <code>IProcessor</code>:</p> <pre><code>public interface IProcessor { void Process(object instance); Type InstanceType {get;} } public interface IProcessor&lt;T&gt; : IProcessor { void Process(T instance); } class SomeClass: IProcessor&lt;int&gt; { public void Process(int instance) { throw new NotImplementedException(); } Type IProcessor.InstanceType {get {return typeof(int);}} void IProcessor.Process(object instance) { Process((int)instance); } } </code></pre> <p>Of course, an (optional) base-class might allow you to avoid some of this per-implementation:</p> <pre><code>abstract class SomeBase&lt;T&gt; : IProcessor&lt;T&gt; { public void Process(T instance) { OnProcess(instance); } Type IProcessor.InstanceType {get {return typeof(T);}} void IProcessor.Process(object instance) { Process((T)instance); } protected abstract void OnProcess(T instance); } </code></pre>
 

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