title: How to create a class dynamicaly
body: <p>I am trying to generate a class dinamicaly through a List of dictionaries with the following code:</p>
  <pre><code>public static object CreateCustomObject(List&lt;Dictionary&lt;string, string&gt;&gt; fileProperties)
  {
      object result = null;
      
      // create the assembly and module
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = &quot;tmpAssembly&quot;;
      AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder module = assemblyBuilder.DefineDynamicModule(&quot;tmpAssembly&quot;);
      
      // create a new type builder
      TypeBuilder typeBuilder = module.DefineType(&quot;CustomObjectToSerialize&quot;, TypeAttributes.Public | TypeAttributes.Class);
      
      foreach (Dictionary&lt;string, string&gt; _property in fileProperties)
      {
          foreach (var _elem in _property)
          {
              // create property from file      
              string propertyName = _elem.Key.Replace('.', '_');
              FieldBuilder field = typeBuilder.DefineField(&quot;_&quot; + propertyName, typeof(string), FieldAttributes.Private);
              PropertyBuilder property = typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, typeof(string), new Type[] { typeof(string) });
      
              MethodAttributes GetSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig;
      
              MethodBuilder currGetPropMethBldr = typeBuilder.DefineMethod(&quot;get_value&quot;, GetSetAttr, typeof(string), Type.EmptyTypes);
      
              ILGenerator currGetIL = currGetPropMethBldr.GetILGenerator();
              currGetIL.Emit(OpCodes.Ldarg_0);
              currGetIL.Emit(OpCodes.Ldfld, field);
              currGetIL.Emit(OpCodes.Ret);
      
              MethodBuilder currSetPropMthdBldr = typeBuilder.DefineMethod(&quot;set_value&quot;, GetSetAttr, typeof(string), Type.EmptyTypes);
      
              ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
              currSetIL.Emit(OpCodes.Ldarg_0);
              currSetIL.Emit(OpCodes.Ldarg_1);
              currSetIL.Emit(OpCodes.Stfld, field);
              currSetIL.Emit(OpCodes.Ret);
      
              property.SetGetMethod(currGetPropMethBldr);
              property.SetSetMethod(currSetPropMthdBldr);
          }
      }
      
      Type generatedType = typeBuilder.CreateType();
      result = Activator.CreateInstance(generatedType);
      PropertyInfo[] properties = generatedType.GetProperties();
      
      int propertiesCount = 0;
      
      List&lt;string&gt; values = new List&lt;string&gt;();
      foreach (Dictionary&lt;string, string&gt; _property in fileProperties)
      {
          foreach (var _elem in _property)
          {
              //values.Add(_elem.Value);
              string value = _elem.Value;
              string propName = _elem.Key.Replace('.', '_');
              PropertyInfo pinfo = generatedType.GetProperty(propName);
              //here I get the error
              //'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll
              pinfo.SetValue(result, value);
              //properties[propertiesCount].SetValue(result, value, null);
              //propertiesCount++;
          }
      }
      
      //values.ForEach(_v =&gt;
      //{
      //   properties[propertiesCount].SetValue(result, _v, null);
      //   propertiesCount++;
      //});
      
      return result;
  }
  </code></pre>
  <p>I get this error when trying to set the value of the property.:</p>
  <p><em>An unhandled exception of type 'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll
  Additional information: Parameter count mismatch.</em></p>
  <p>I checked and I have in my sample 3 properties and I will assign 3 values.</p>
  <p>The code is taken and Little bit adapted from <a href="http://mironabramson.com/blog/post/2008/06/Create-you-own-new-Type-and-use-it-on-run-time-(C).aspx" rel="nofollow noreferrer">here</a></p>
  <p>Any idea?</p>