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.
I am using ShouldSerialize for this situation but I still have so many fields and types.
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.
[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 => CommandControls.IfBNeeded(Id);
public int TypeBField1 { get; set; }
public bool ShouldSerializeTypeBField2 => CommandControls.IfBNeeded(Id);
public int TypeBField2 { get; set; }
public bool ShouldSerializeTypeBField3 => CommandControls.IfBNeeded(Id);
public int TypeBField3 { get; set; }
...
public bool ShouldSerializeTypeCField1 => CommandControls.IfCNeeded(Id);
public int TypeCField1 { get; set; }
public bool ShouldSerializeTypeCField2 => CommandControls.IfCNeeded(Id);
public int TypeCField2 { get; set; }
public bool ShouldSerializeTypeCField3 => CommandControls.IfCNeeded(Id);
public int TypeCField3 { get; set; }
...
}
I would like to write something like this
public bool ShouldSerializeTypeBFields => CommandControls.IfBNeeded(Id);
public bool ShouldSerializeTypeCFields => CommandControls.IfCNeeded(Id);
Note: Field names don't actually start with TypeBField
; that's a placeholder for a normal field name like Phone, Address, etc.