Note that there are some explanatory texts on larger screens.

plurals
  1. POOpen delegate for generic interface method
    primarykey
    data
    text
    <p>I'm trying to create an <a href="http://peisker.net/dotnet/languages2005.htm#delegatetargets" rel="noreferrer">open instance delegate</a> for a generic interface method, but I keep receiving a NotSupportedException. Here is the simplified code that won't run:</p> <pre><code>interface IFoo { void Bar&lt;T&gt;(T j); } class Foo : IFoo { public void Bar&lt;T&gt;(T j) { } } static void Main(string[] args) { var bar = typeof(IFoo).GetMethod("Bar").MakeGenericMethod(typeof(int)); var x = Delegate.CreateDelegate(typeof(Action&lt;IFoo, int&gt;), null, bar); } </code></pre> <p>The last line throws NotSupportedException, "Specified method is not supported". By comparison, a non-generic open instance delegate runs fine:</p> <pre><code>interface IFoo { void Bar(int j); } class Foo : IFoo { public void Bar(int j) { } } static void Main(string[] args) { var bar = typeof(IFoo).GetMethod("Bar"); var x = Delegate.CreateDelegate(typeof(Action&lt;IFoo, int&gt;), null, bar); } </code></pre> <p>And a closed generic delegate also works:</p> <pre><code>interface IFoo { void Bar&lt;T&gt;(T j); } class Foo : IFoo { public void Bar&lt;T&gt;(T j) { } } static void Main(string[] args) { var bar = typeof(IFoo).GetMethod("Bar").MakeGenericMethod(typeof(int)); var x = Delegate.CreateDelegate(typeof(Action&lt;int&gt;), new Foo(), bar); } </code></pre> <p>So the recipe for closed generic delegates and open instance delegates work separately, but not when combined. It's starting to look like either a runtime bug, or intentional omission. Anyone have any insight here?</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