Note that there are some explanatory texts on larger screens.

plurals
  1. POReflection-generated and generic types
    primarykey
    data
    text
    <p>I'm having yet another nasty moment with <code>Reflection.Emit</code> and type management.</p> <p>Say, I have a type named <code>MyType</code> which is defined in the dynamically generated assembly. Calling <code>MyType.GetMethods()</code> results in a <code>NotSupportedException</code>, which has reduced me to writing my own set of wrappers and lookup tables. However, the same is happening when I'm calling <code>GetMethods()</code> or any other introspecting methods on standard generic types which use my own types as generic arguments:</p> <ul> <li><code>Tuple&lt;int, string&gt;</code> => works fine</li> <li><code>Tuple&lt;int, MyType&gt;</code> => exception</li> </ul> <p>I can get the method list from the generic type definition:</p> <p><code>typeof(Tuple&lt;int, MyType).GetGenericTypeDefinition().GetMethods()</code></p> <p>However, the methods have generic placeholders instead of actual values (like <code>T1</code>, <code>TResult</code> etc.) and I don't feel like writing yet another kludge that traces the generic arguments back to their original values.</p> <p>A sample of code:</p> <pre><code>var asmName = new AssemblyName("Test"); var access = AssemblyBuilderAccess.Run; var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, access); var module = asm.DefineDynamicModule("Test"); var aType = module.DefineType("A"); var tupleType = typeof(Tuple&lt;,&gt;); var tuple = tupleType.MakeGenericType(new [] { typeof(int), aType }); tuple.GetProperty("Item1"); // &lt;-- here's the error </code></pre> <p>So the questions are:</p> <ol> <li>How do I detect if a type is safe to call <code>GetMethods()</code> and similar methods on?</li> <li>How do I get the actual list of methods and their generic argument values, if the type is not safe?</li> </ol>
    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.
    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