Here it is one attempt more, this time following Dr. Dobbs naming pattern,
now I realize what Sergei could mean when saying saving twice,the first statically defined Simpleclass, and the second dynamically defined SimpleClass are different,because they live in different namespaces,SimpleDynamic and SimpleDyn respectively,in the previous snippets was not clear cause the top namespace declaration was not visible,however in this attempt I have renamed the dynamically built class to SimpleClass1,the purpose of building these two identical classes side by side is to compare them using ildasm.
namespace SimpleDynamic
{
class Program
{
public class SimpleClass
{
public string Name;
public int Age;
}
static void Main(string[] args)
{
AppDomain appDomain = AppDomain.CurrentDomain;
AssemblyName aName = new AssemblyName();
aName.Name = "SimpleReflection";
AssemblyBuilder assemBuilder = appDomain.DefineDynamicAssembly(aName,
AssemblyBuilderAccess.Save);
ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule(aName.Name,
"SimpleDyn.dll");
TypeBuilder tb = modBuilder.DefineType("SimpleDyn.SimpleClass1",
TypeAttributes.Public | TypeAttributes.BeforeFieldInit, typeof(object));
FieldBuilder field1 = tb.DefineField("Name", typeof(string), FieldAttributes.Public);
FieldBuilder field2 = tb.DefineField("Age", typeof(int), FieldAttributes.Public);
tb.CreateType();
assemBuilder.Save("SimpleDyn.dll");
}
}