I have classes like that:
public partial class InvoiceType
{
private UBLVersionIDType uBLVersionIDField;
private CopyIndicatorType copyIndicatorField;
private CustomerPartyType accountingCustomerPartyField;
}
public partial class CustomerPartyType
{
private PartyType partyField;
private ContactType deliveryContactField;
/// <remarks/>
public PartyType Party
{
get
{
return this.partyField;
}
set
{
this.partyField = value;
}
}
/// <remarks/>
public ContactType DeliveryContact
{
get
{
return this.deliveryContactField;
}
set
{
this.deliveryContactField = value;
}
}
}
When I want to generate a new InvoiceType I use that code:
var invoice = new InvoiceType()
{
UBLVersionID = new UBLVersionIDType() { Value = "2.1" },
CopyIndicator = new CopyIndicatorType() { Value = false },
};
If I want to try to set
invoice.AccountingCustomerParty.Party.PartyIdentification = myPartyIdentification;
it occurs NullReferenceException. Because I didn't create AccountingCustomerParty and Party and also PartyIdentification yet. I must to create them with new keyword for first time to set value.
I can skip these steps with deserialize a draft invoice json at first, then all properties are created but I don't want to use deserialize method because it creates all properties. I want to create just my used properties.
Is there another way from deserialization method to set sub classes and their properties without using new keyword?