title: ShouldSerialize for Multiple Fields
body: <p>I'm trying to communicate with a system (using xml) that requires certain optional fields depending on the value of another field. If I send an unneeded one, the system returns an error. </p>
  
  <p>I am using ShouldSerialize for this situation but I still have so many fields and types.</p>
  
  <p>Is there an easy way to do this or is this the wrong way to go about this? What should I use? I am writing so much duplicated code and this feels wrong. </p>
  
  <pre><code>[Serializable()]
  public class AEntity
  {
      public int Id { get; set; }
      public int All1 { get; set; }
      public int All2 { get; set; }
      public int All3 { get; set; }
  
  
      public bool ShouldSerializeTypeBField1 =&gt; CommandControls.IfBNeeded(Id);
      public int TypeBField1 { get; set; }
  
      public bool ShouldSerializeTypeBField2 =&gt; CommandControls.IfBNeeded(Id);
      public int TypeBField2 { get; set; }
  
      public bool ShouldSerializeTypeBField3 =&gt; CommandControls.IfBNeeded(Id);
      public int TypeBField3 { get; set; }
      ...
  
      public bool ShouldSerializeTypeCField1 =&gt; CommandControls.IfCNeeded(Id);
      public int TypeCField1 { get; set; }
  
      public bool ShouldSerializeTypeCField2 =&gt; CommandControls.IfCNeeded(Id);
      public int TypeCField2 { get; set; }
  
      public bool ShouldSerializeTypeCField3 =&gt; CommandControls.IfCNeeded(Id);
      public int TypeCField3 { get; set; }
      ...
  
  
  }
  </code></pre>
  
  <p>I would like to write something like this</p>
  
  <pre><code>public bool ShouldSerializeTypeBFields =&gt; CommandControls.IfBNeeded(Id);
  public bool ShouldSerializeTypeCFields =&gt; CommandControls.IfCNeeded(Id);
  </code></pre>
  
  <p><em>Note: Field names don't actually start with <code>TypeBField</code>; that's a placeholder for a normal field name like Phone, Address, etc.</em></p>