Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From what I can tell, generic parameters never get passed to the <code>TryGetMember()</code> or <code>TryInvokeMember</code> overrides. The MSDN documentation only shows simple string names getting passed in by the <code>GetMemberBinder</code> and <code><a href="http://msdn.microsoft.com/en-us/library/system.dynamic.invokememberbinder.aspx" rel="nofollow">GetInvokeBinder</a></code> objects.</p> <p>The most simple implementation is to actually implement <code>GetValues&lt;T&gt;()</code> on the <code>DynamicJSON</code> class.</p> <pre><code>public List&lt;T&gt; GetValues&lt;T&gt;() { Console.WriteLine("Called with generic parameter {0}", typeof(T)); return new List&lt;T&gt;(); } </code></pre> <p>When I ran this simple test, the generic parameter was a .NET <code>Int32</code> type:</p> <pre><code>dynamic json = new DynamicJSON(); json.GetValues&lt;int&gt;() </code></pre> <p>If implementing the <code>GetValues&lt;T&gt;()</code> method is not feasible for you, you can implement <code>TryInvokeMember()</code> to return a method that has <code><a href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.tryconvert.aspx" rel="nofollow">TryConvert()</a></code> implemented. When .NET tries to convert the result of <code>GetValues&lt;int&gt;()</code> into a <code>List&lt;int&gt;()</code>, your overridden <code>TryConvert()</code> method will get called with <code>List&lt;int&gt;()</code> as the "convert binder type".</p> <pre><code>public class DynamicJSON : DynamicObject { public override bool TryInvokeMember(InvokeMemberBinder binder, Object[] args, out Object result) { if (binder.Name == "GetValues") { result = new DynamicValues(); return true; } else { result = null; return false; } } private class DynamicValues : DynamicObject { public override bool TryConvert(ConvertBinder binder, out Object result) { Console.WriteLine("Converting dynamic values to {0}", binder.Type); if (binder.Type == typeof(List&lt;int&gt;)) { result = new List&lt;int&gt;(); return true; } else { result = null; return false; } } } } public class Test { public static void Main(string[] args) { dynamic json = new DynamicJSON(); List&lt;int&gt; values = json.GetValues(); Console.WriteLine(values.GetType()); } } </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.
    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