Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As far as I can understand in this case <code>Foo&lt;T&gt;</code> is an open unbound generic type, so at runtime the CLR will use it as a blueprint/skeleton to construct and close a generic type with the type parameter type specified (<code>Foo&lt;int&gt;</code>, <code>Foo&lt;object&gt;</code>, etc.). So basically <code>Foo&lt;int&gt;</code> is a runtime constructed implementation of the <code>Foo&lt;T&gt;</code> skeleton.</p> <p>Now, at run-time you can get the type of <code>Foo&lt;int&gt;</code> either by using <code>typeof(Foo&lt;int&gt;)</code> or <code>typeof(Foo&lt;&gt;).MakeGenericType(new[] { typeof(int) })</code> and it's not the same <code>Type</code> and it wouldn't make sense for it to be. But look closer and you will see that both <code>typeof(Foo&lt;T&gt;)</code> and <code>typeof(Foo&lt;int&gt;)</code> share the same metadata token and GUID. </p> <p>Another interesting thing is that <code>typeof(Foo&lt;int&gt;).Assembly</code> will be what you would expect, but as you've noticed already you can't get that type from the Assembly. </p> <p>That's because <code>Foo&lt;int&gt;</code> is not defined in the assembly (you can check the assembly metadata with Reflector/ILSpy). At run-time the CLR will create ("construct") a specialized ("closed") version of the <code>Foo&lt;T&gt;</code> for <code>Foo&lt;int&gt;</code> (so - constructed closed type of an unbounded open generic type definition) and "give" it a <code>Type</code>. So unless the CLR exposes directly somehow the list of closed generic types it generates at run-time you are out of luck.</p> <p>Also here is a snippet that might confirm what I am saying:</p> <blockquote> <p>Even though each construction of a generic type, such as Node&lt; Form > and Node&lt; String >, has its own distinct type identity, the CLR is able to reuse much of the actual JIT-compiled code between the type instantiations. This drastically reduces code bloat and is possible because the various instantiations of a generic type are expanded at run time. All that exists of a constructed type at compile time is a type reference. When assemblies A and B both reference a generic type defined in a third assembly, their constructed types are expanded at run time. This means that, in addition to sharing CLR type-identities (when appropriate), type instantiations from assemblies A and B also share run-time resources such as native code and expanded metadata.</p> <p><a href="http://msdn.microsoft.com/en-us/magazine/cc163683.aspx">http://msdn.microsoft.com/en-us/magazine/cc163683.aspx</a></p> </blockquote>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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