سؤال

i need to return an array of a structure in a Datacontract. i cant manage to make it. i receive an error when setting the values for the array.

Here's the Datacontract declaration:

    [DataContract] 
    public class invoice_data
    {
        [DataMember]
        public Invoice_Body_Item[] invoice_body;
    }


    [StructLayout(LayoutKind.Sequential)]
    public struct Invoice_Body_Item
    {
        public string Item_Description;
        public decimal Item_Value;
    }
}

And here's the method code:

invoice_data Invoice = new invoice_data();
object tr_bl = svr.GetInvoiceData(inputparams.ck, svr.Confirm(inputparams.ck));

for (int i = ((Array)(((object[])(tr_bl))[1])).GetLowerBound(0); i <= ((Array)(((object[])(tr_bl))[1])).GetUpperBound(0); i++)
{
    Invoice.invoice_body[i].Item_Description = (string)((object[])(((object[])(((object[])(tr_bl))[1]))[i]))[0];
    Invoice.invoice_body[i].Item_Value = (decimal)((object[])(((object[])(((object[])(tr_bl))[1]))[i]))[1]; 
}
                        }

In this line i get the error "Object reference not set to an instance of an object."

Invoice.invoice_body[i].Item_Description = (string)((object[])(((object[])(((object[])(tr_bl))[1]))[i]))[0];
هل كانت مفيدة؟

المحلول

Your struct should also be decorated with a [DataContract] + Decorate it's members with a [DataMember]:

[StructLayout(LayoutKind.Sequential)]
[DataContract]
public struct Invoice_Body_Item
{
    [DataMember]
    public string Item_Description;
    [DataMember]
    public decimal Item_Value;
}

Alternatively, you can use the [KnownTypeAttribute(typeof(Invoice_Body_Item))]

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top