title: How To Minimize Assembly Size On Disk Of Generated Code?
body: <p>I'm generating DTO objects for an online platform (Dynamics 365).  There are pretty strict constraints on maximum assembly size, and just the generated DTO objects take up about 80% of that limit.  What changes can I make to the code that is being generated to decrease the amount of space on disk the compiled assembly takes up?</p>
  
  <p>Here is an example class that is generated:</p>
  
  <pre class="lang-cs prettyprint-override"><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated by a tool.
  //
  //     Changes to this file may cause incorrect behavior and will be lost if
  //     the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  namespace Contoso.Xrm.Entities
  {
  
      /// &lt;summary&gt;
      /// Track changes to records for analysis, record keeping, and compliance.
      /// &lt;/summary&gt;
      [System.Runtime.Serialization.DataContractAttribute()]
      [Microsoft.Xrm.Sdk.Client.EntityLogicalNameAttribute("audit")]
      [System.CodeDom.Compiler.GeneratedCodeAttribute("CrmSvcUtil", "8.2.1.8676")]
      public partial class Audit : Microsoft.Xrm.Sdk.Entity, System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
      {
  
          public static class Fields
          {
              public const string Action = "action";
              public const string AttributeMask = "attributemask";
              public const string AuditId = "auditid";
              public const string Id = "auditid";
              public const string CallingUserId = "callinguserid";
              public const string CreatedOn = "createdon";
              public const string ObjectId = "objectid";
              public const string Operation = "operation";
              public const string RegardingObjectId = "regardingobjectid";
              public const string TransactionId = "transactionid";
              public const string UserAdditionalInfo = "useradditionalinfo";
              public const string UserId = "userid";
              public const string lk_audit_callinguserid = "lk_audit_callinguserid";
              public const string lk_audit_userid = "lk_audit_userid";
          }
  
  
          /// &lt;summary&gt;
          /// Default Constructor.
          /// &lt;/summary&gt;
          [System.Diagnostics.DebuggerNonUserCode()]
          public Audit() : 
                  base(EntityLogicalName)
          {
          }
  
          public const string EntityLogicalName = "audit";
  
          public const string PrimaryIdAttribute = "auditid";
  
          public const int EntityTypeCode = 4567;
  
          public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
  
          public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;
  
          [System.Diagnostics.DebuggerNonUserCode()]
          private void OnPropertyChanged(string propertyName)
          {
              if ((this.PropertyChanged != null))
              {
                  this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
              }
          }
  
          [System.Diagnostics.DebuggerNonUserCode()]
          private void OnPropertyChanging(string propertyName)
          {
              if ((this.PropertyChanging != null))
              {
                  this.PropertyChanging(this, new System.ComponentModel.PropertyChangingEventArgs(propertyName));
              }
          }
  
          /// &lt;summary&gt;
          /// Actions the user can perform that cause a change
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("action")]
          public Microsoft.Xrm.Sdk.OptionSetValue Action
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;Microsoft.Xrm.Sdk.OptionSetValue&gt;("action");
              }
          }
  
          /// &lt;summary&gt;
          /// Contains a CSV of the ColumnNumber metadata property of attributes
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("attributemask")]
          public string AttributeMask
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;string&gt;("attributemask");
              }
          }
  
          /// &lt;summary&gt;
          /// Unique identifier of the auditing instance
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("auditid")]
          public System.Nullable&lt;System.Guid&gt; AuditId
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;System.Nullable&lt;System.Guid&gt;&gt;("auditid");
              }
          }
  
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("auditid")]
          public override System.Guid Id
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return base.Id;
              }
              [System.Diagnostics.DebuggerNonUserCode()]
              set
              {
                  base.Id = value;
              }
          }
  
          /// &lt;summary&gt;
          /// Unique identifier of the calling user in case of an impersonated call
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("callinguserid")]
          public Microsoft.Xrm.Sdk.EntityReference CallingUserId
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;Microsoft.Xrm.Sdk.EntityReference&gt;("callinguserid");
              }
          }
  
          /// &lt;summary&gt;
          /// Date and time when the audit record was created.
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("createdon")]
          public System.Nullable&lt;System.DateTime&gt; CreatedOn
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;System.Nullable&lt;System.DateTime&gt;&gt;("createdon");
              }
          }
  
          /// &lt;summary&gt;
          /// Unique identifier of the record that is being audited
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("objectid")]
          public Microsoft.Xrm.Sdk.EntityReference ObjectId
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;Microsoft.Xrm.Sdk.EntityReference&gt;("objectid");
              }
          }
  
          /// &lt;summary&gt;
          /// The action that causes the audit--it will be create, delete, or update
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("operation")]
          public Microsoft.Xrm.Sdk.OptionSetValue Operation
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;Microsoft.Xrm.Sdk.OptionSetValue&gt;("operation");
              }
          }
  
          /// &lt;summary&gt;
          /// Unique identifier of the object with which the record is associated.
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("regardingobjectid")]
          public Microsoft.Xrm.Sdk.EntityReference RegardingObjectId
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;Microsoft.Xrm.Sdk.EntityReference&gt;("regardingobjectid");
              }
              [System.Diagnostics.DebuggerNonUserCode()]
              set
              {
                  this.OnPropertyChanging("RegardingObjectId");
                  this.SetAttributeValue("regardingobjectid", value);
                  this.OnPropertyChanged("RegardingObjectId");
              }
          }
  
          /// &lt;summary&gt;
          /// Unique identifier for multiple changes that are part of a single operation; this field contains the same GUID for all the audit rows generated in a single transaction
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("transactionid")]
          public System.Nullable&lt;System.Guid&gt; TransactionId
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;System.Nullable&lt;System.Guid&gt;&gt;("transactionid");
              }
          }
  
          /// &lt;summary&gt;
          /// Additional information associated to the user who caused the change.
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("useradditionalinfo")]
          public string UserAdditionalInfo
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;string&gt;("useradditionalinfo");
              }
              [System.Diagnostics.DebuggerNonUserCode()]
              set
              {
                  this.OnPropertyChanging("UserAdditionalInfo");
                  this.SetAttributeValue("useradditionalinfo", value);
                  this.OnPropertyChanged("UserAdditionalInfo");
              }
          }
  
          /// &lt;summary&gt;
          /// Unique identifier of the user who caused a change
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("userid")]
          public Microsoft.Xrm.Sdk.EntityReference UserId
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetAttributeValue&lt;Microsoft.Xrm.Sdk.EntityReference&gt;("userid");
              }
          }
  
          /// &lt;summary&gt;
          /// 1:N userentityinstancedata_audit
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("userentityinstancedata_audit")]
          public System.Collections.Generic.IEnumerable&lt;Contoso.Xrm.Entities.UserEntityInstanceData&gt; userentityinstancedata_audit
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetRelatedEntities&lt;Contoso.Xrm.Entities.UserEntityInstanceData&gt;("userentityinstancedata_audit", null);
              }
              [System.Diagnostics.DebuggerNonUserCode()]
              set
              {
                  this.OnPropertyChanging("userentityinstancedata_audit");
                  this.SetRelatedEntities&lt;Contoso.Xrm.Entities.UserEntityInstanceData&gt;("userentityinstancedata_audit", null, value);
                  this.OnPropertyChanged("userentityinstancedata_audit");
              }
          }
  
          /// &lt;summary&gt;
          /// N:1 lk_audit_callinguserid
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("callinguserid")]
          [Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("lk_audit_callinguserid")]
          public Contoso.Xrm.Entities.SystemUser lk_audit_callinguserid
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetRelatedEntity&lt;Contoso.Xrm.Entities.SystemUser&gt;("lk_audit_callinguserid", null);
              }
          }
  
          /// &lt;summary&gt;
          /// N:1 lk_audit_userid
          /// &lt;/summary&gt;
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("userid")]
          [Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("lk_audit_userid")]
          public Contoso.Xrm.Entities.SystemUser lk_audit_userid
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return this.GetRelatedEntity&lt;Contoso.Xrm.Entities.SystemUser&gt;("lk_audit_userid", null);
              }
          }
  
          /// &lt;summary&gt;
          /// Constructor for populating via LINQ queries given a LINQ anonymous type
          /// &lt;param name="anonymousType"&gt;LINQ anonymous type.&lt;/param&gt;
          /// &lt;/summary&gt;
          [System.Diagnostics.DebuggerNonUserCode()]
          public Audit(object anonymousType) : 
                  this()
          {
              foreach (var p in anonymousType.GetType().GetProperties())
              {
                  var value = p.GetValue(anonymousType, null);
                  var name = p.Name.ToLower();
  
                  if (name.EndsWith("enum") &amp;&amp; value.GetType().BaseType == typeof(System.Enum))
                  {
                      value = new Microsoft.Xrm.Sdk.OptionSetValue((int) value);
                      name = name.Remove(name.Length - "enum".Length);
                  }
  
                  switch (name)
                  {
                      case "id":
                          base.Id = (System.Guid)value;
                          Attributes["auditid"] = base.Id;
                          break;
                      case "auditid":
                          var id = (System.Nullable&lt;System.Guid&gt;) value;
                          if(id == null){ continue; }
                          base.Id = id.Value;
                          Attributes[name] = base.Id;
                          break;
                      case "formattedvalues":
                          // Add Support for FormattedValues
                          FormattedValues.AddRange((Microsoft.Xrm.Sdk.FormattedValueCollection)value);
                          break;
                      default:
                          Attributes[name] = value;
                          break;
                  }
              }
          }
  
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("action")]
          public virtual Audit_Action? ActionEnum
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return ((Audit_Action?)(EntityOptionSetEnum.GetEnum(this, "action")));
              }
          }
  
          [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("operation")]
          public virtual Audit_Operation? OperationEnum
          {
              [System.Diagnostics.DebuggerNonUserCode()]
              get
              {
                  return ((Audit_Operation?)(EntityOptionSetEnum.GetEnum(this, "operation")));
              }
          }
      }
  }
  </code></pre>