title: _COMPlusExceptionCode -532462766 While creating ModuleBuilder.DefineType
body: <p>I am trying to create a class dynamically based on a base class type.</p>
  
  <p>Below is the code which causing problem</p>
  
  <pre><code>     TypeBuilder tb = DynamicType.CreateType(DynamicType.modBuilder, typeof(ResultViewModelVersioned).ToString());
  </code></pre>
  
  <p>The function for create type is as follow</p>
  
  <pre><code>     public static TypeBuilder CreateType(ModuleBuilder modBuilder, string typeName)
      {
          TypeBuilder typeBuilder = modBuilder.DefineType(typeName,
                      TypeAttributes.Public );
          return typeBuilder;
      }
  </code></pre>
  
  <p>But i am getting com exception _COMPlusExceptionCode    -532462766  while defining type in above function.</p>
  
  <p>Here is my function which creates dynamic object</p>
  
  <pre><code>public object GetViewModel&lt;TresultType, TviewModelType&gt;(TresultType result, TviewModelType ViewModel)
  {
      if (DynamicType.asmBuilder == null)
          DynamicType.GenerateAssemblyAndModule();
      var finalType = DynamicType.modBuilder.GetType("Beacon11");
  
      TypeBuilder tb = DynamicType.CreateType(DynamicType.modBuilder, ViewModel.GetType().ToString());
      tb.SetParent(typeof(ResultViewModelVersionable));
  
      var sourceType = result.GetType();
      var targetType = tb.GetType();
  
      foreach (var property in sourceType.GetProperties())
      {
          var targetProperty = targetType.GetProperty(property.Name);
          if (targetProperty == null)
          {  
              DynamicType.CreateProperty(tb, property.Name, property.GetType());
          }
      }
  
      finalType = tb.CreateType();
      var Methods = tb.GetMethods();
      Object obj = Activator.CreateInstance(finalType);
      return obj;
  }
  </code></pre>
  
  <p>I got code to create DynamicType From the below link</p>
  
  <p><a href="http://geekswithblogs.net/rgupta/archive/2008/12/01/dynamically-creating-types-using-reflection-and-setting-properties-using-reflection.emit.aspx" rel="nofollow">http://geekswithblogs.net/rgupta/archive/2008/12/01/dynamically-creating-types-using-reflection-and-setting-properties-using-reflection.emit.aspx</a></p>
  
  <p>Any help would be greatly appreciated. If you need any further information please let me know.</p>