Skip to content

Instantly share code, notes, and snippets.

@lambdageek
Created August 23, 2016 17:06
Show Gist options
  • Save lambdageek/e57813c49106913389a7895e4bf24b49 to your computer and use it in GitHub Desktop.
Save lambdageek/e57813c49106913389a7895e4bf24b49 to your computer and use it in GitHub Desktop.
How to hit reflection.c:2178
using System;
using System.Reflection;
using System.Reflection.Emit;
class TestRefl {
public static void Main (string[] args)
{
var ab = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Test"), AssemblyBuilderAccess.Run);
var mb = ab.DefineDynamicModule ("Test");
var tb = mb.DefineType ("TestG", TypeAttributes.Public);
var gp = tb.DefineGenericParameters (new [] { "T" });
var gparam = gp[0];
var fb = tb.DefineField ("Foo", gparam, FieldAttributes.Public);
var inst = tb.MakeGenericType (new [] {typeof (Int32)});
var fig = tb.GetField ("Foo", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public);
FieldInfo fii = TypeBuilder.GetField (inst, fig); // inst.GetField ("Foo");
var t = tb.CreateType ();
var ti = t.MakeGenericType (new [] {typeof (Int32)});
var fti = TypeBuilder.GetField (ti, fig);
//var tok = fii.MetadataToken;
Console.WriteLine ("ab {0}", ab);
Console.WriteLine ("mb {0}", mb);
Console.WriteLine ("tb {0}", tb);
Console.WriteLine ("gparam {0}", gparam);
//Console.WriteLine ("fb {0}", fb);
Console.WriteLine ("fig {0}", fig);
Console.WriteLine ("inst {0} : {1}", inst, inst.GetType());
Console.WriteLine ("fii {0} : {1}", fii, fii.GetType ());
//Console.WriteLine ("tok {0}", tok);
Console.WriteLine ("t {0} : {1}", t, t.GetType ());
Console.WriteLine ("ti {0} : {1}", ti, ti.GetType ());
Console.WriteLine ("fti {0} : {1}" , fti, fti.GetType ());
Console.WriteLine ("fti.MetadataToken {0}", fti.MetadataToken);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment