c#调用方法中的序列化

时间:2015-06-04 13:04:15

标签: c# vb.net network-programming

此代码来自使用C#和VB.net的#net;网络编程。#34;

我不明白的是如何 正在设置_issuanceDate,_deliveryDate,_invoiceDate,_paymentDate。在代码中没有明确的值设置,但XML文件包含所有日期,以及如何调用purchaseorder类方法?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.Serialization.Formatters.Soap;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;

    namespace Serialization
    {
         public partial class Form1 : Form
         {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        company vendor = new company();
        company buyer = new company();
        lineItem Goods = new lineItem();
        purchaseOrder po = new purchaseOrder();

        vendor.name = "Acme";
        buyer.name = "Wiley E Coyete";

        Goods.description = "Anti Road Runner Gun";
        Goods.quantity = 1;
        Goods.cost = 599.99;

        po.items = new lineItem[1];
        po.items[0] = Goods;
        po.buyer = buyer;
        po.vendor = vendor;



        SoapFormatter sf = new SoapFormatter();
        FileStream fs = File.Create("C:\\Users\\S Ghosh\\Desktop\\po.xml");
        sf.Serialize(fs, po);
        fs.Close();            
    }

    private void button2_Click(object sender, EventArgs e)
    {
        SoapFormatter sf = new SoapFormatter();
        FileStream fs = File.OpenRead("C:\\Users\\S Ghosh\\Desktop\\po.xml");
        purchaseOrder po = (purchaseOrder)sf.Deserialize(fs);
        fs.Close();
        MessageBox.Show("Customer is " + po.buyer.name);
        po.show();
    }
}

public enum purchaseOrderStates
{

    DELIVERED,
    ISSUED,
    INVOICED,
    PAID
}

[Serializable()]
public class company
{
    public string name;
    public string address;
    public string phone;
}

[Serializable()]
public class lineItem
{
    public string description;
    public int quantity;
    public double cost;
}
[Serializable()]
public class purchaseOrder
{
    private purchaseOrderStates _purchaseOrderStatus;
    private DateTime _issuanceDate;
    private DateTime _deliveryDate;
    private DateTime _invoiceDate;
    private DateTime _paymentDate;

    public company buyer;
    public company vendor;
    public company reference;

    public lineItem[] items;
    public purchaseOrder()
    {
        _purchaseOrderStatus = purchaseOrderStates.ISSUED;
        _issuanceDate = DateTime.Now;
    }

    public void recordDelivery()
    {
        if (_purchaseOrderStatus == purchaseOrderStates.ISSUED)
        {
            _purchaseOrderStatus = purchaseOrderStates.DELIVERED;
            _deliveryDate = DateTime.Now;
        }
    }

    public void recordInvoice()
    {
        if (_purchaseOrderStatus == purchaseOrderStates.DELIVERED)
        {
            _purchaseOrderStatus = purchaseOrderStates.INVOICED;
            _invoiceDate = DateTime.Now;
        }
    }

    public void recordPayment()
    {
        if (_purchaseOrderStatus == purchaseOrderStates.INVOICED)
        {
            _purchaseOrderStatus = purchaseOrderStates.PAID;
            _issuanceDate = DateTime.Now;
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

_issuanceDate在purchaseOrder构造函数中设置

_issuanceDate = DateTime.Now;

所有其他日期都使用默认值。

答案 1 :(得分:0)

当您在purchaseOrder po = new purchaseOrder();中声明purchaseOrder private void button1_Click(object sender, EventArgs e)的实例时,这将调用purchaseOrder构造函数:

public purchaseOrder()
{
    _purchaseOrderStatus = purchaseOrderStates.ISSUED;
    _issuanceDate = DateTime.Now;
}

这将启动_purchaseOrderStatus_issuanceDate