title: Creating dynamic type from TypeBuilder with a base class and additional fields generates an exception
body: <p>I am trying to create a dynamic type based on an existing type that contains only public fields.  The new dynamic type must also inherit from a different base type which only has a fully implemented method.</p>
  
  <p>I create the <code>TypeBuilder</code> specifying the base type then I add the public fields to it and finally I call <code>CreateType()</code>.  The resulting error message is:</p>
  
  <blockquote>
    <p>"Could not load type 'InternalType' from assembly 'MyDynamicAssembly,
    Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because field
    'first' was not given an explicit offset."</p>
  </blockquote>
  
  <p>To me this implies that the <code>CreateType</code> method is looking for the public field "first" in the base class which is a problem because it is not there.  Why does it think the added field should be in the base class?  Or, am I misunderstanding the exception?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public class sourceClass
  {
      public Int32 first = 1;
      public Int32 second = 2;
      public Int32 third = 3;
  }
  
  public static class MyConvert
  {
      public static object ToDynamic(object sourceObject, out Type outType)
      {
          // get the public fields from the source object
          FieldInfo[] sourceFields = sourceObject.GetType().GetFields();
  
          // get a dynamic TypeBuilder and inherit from the base type
          AssemblyName assemblyName
              = new AssemblyName("MyDynamicAssembly");
          AssemblyBuilder assemblyBuilder
              = AppDomain.CurrentDomain.DefineDynamicAssembly(
                  assemblyName,
                  AssemblyBuilderAccess.Run);
          ModuleBuilder moduleBuilder
              = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
          TypeBuilder typeBuilder
              = moduleBuilder.DefineType(
                  "InternalType",
                  TypeAttributes.Public
                  | TypeAttributes.Class
                  | TypeAttributes.AutoClass
                  | TypeAttributes.AnsiClass
                  | TypeAttributes.ExplicitLayout,
                  typeof(SomeOtherNamespace.MyBase));
  
          // add public fields to match the source object
          foreach (FieldInfo sourceField in sourceFields)
          {
              FieldBuilder fieldBuilder
                  = typeBuilder.DefineField(
                      sourceField.Name,
                      sourceField.FieldType,
                      FieldAttributes.Public);
          }
  
          // THIS IS WHERE THE EXCEPTION OCCURS
          // create the dynamic class
          Type dynamicType = typeBuilder.CreateType();
  
          // create an instance of the class
          object destObject = Activator.CreateInstance(dynamicType);
  
          // copy the values of the public fields of the
          // source object to the dynamic object
          foreach (FieldInfo sourceField in sourceFields)
          {
              FieldInfo destField
                  = destObject.GetType().GetField(sourceField.Name);
              destField.SetValue(
                  destObject,
                  sourceField.GetValue(sourceField));
          }
  
          // give the new class to the caller for casting purposes
          outType = dynamicType;
  
          // return the new object
          return destObject;
      }
  </code></pre>