0

我正在使用 .NET Web API,当然还返回序列化为 JSON 的类。到目前为止,我还没有为任何类使用 Data Contract 属性,但是对于下面的这个类,我这样做了,我不知道为什么。Intellitrace 只是说该类无法序列化并尝试添加 DataContract 属性。我会但想知道为什么。

public class Card : BaseGridVM
{

    private IEnumerable<Pc> _pcCards;
    private IEnumerable<Pt> _ptCards;
    private IEnumerable<MembershipCard> _membershipCards;

    public Grid.Result Pt
    {
        get { return GetPtCardGrid(); }
    }
    public Grid.Result Pc
    {
        get { return GetPcCardGrid(); }
    }
    public Grid.Result Membership
    {
        get { return GetMembershipCardGrid(); }
    }

    public Card(IEnumerable<Pc> pcCards, IEnumerable<Pt> ptCards, IEnumerable<MembershipCard> membershipCards)
    {
        _pcCards = pcCards;
        _ptCards = ptCards;
        _membershipCards = membershipCards;
    }

    private Grid.Result GetPtCardGrid()
    {
        var headers = new List<Grid.Header>
                          {
                              new Grid.Header() {label = "Card Name", width = 250},
                              new Grid.Header() {label = "Pts", width = 50},
                              new Grid.Header() {label = "Activation Date", width = 50}
                          };

        var rows = new List<Grid.Row>();
        foreach (var card in _ptCards)
        {
            var row = new Grid.Row
            {
                id = card.id,
                enabled = card.active.HasValue && (bool)card.active,
                cell = new string[3]
            };

            row.cell[0] = card.cardName;
            row.cell[1] = card.pts.HasValue ? card.pts.ToString() : "0";
            row.cell[2] = card.activationDate.HasValue ? card.activationDate.ToString() : "-";

            rows.Add(row);
        }

        return buildGrid(rows, headers, 1, 3);
    }

    private Grid.Result GetPcCardGrid()
    {
        var headers = new List<Grid.Header>
                          {
                              new Grid.Header() {label = "Offer Title", width = 200},
                              new Grid.Header() {label = "Pces Required", width = 70},
                              new Grid.Header() {label = "Activation Date", width = 50}
                          };

        var rows = new List<Grid.Row>();
        foreach (var card in _pcCards)
        {
            var row = new Grid.Row
            {
                id = card.id,
                enabled = card.active.HasValue && (bool)card.active,
                cell = new string[3]
            };

            row.cell[0] = card.cardName;
            row.cell[1] = card.pces.HasValue ? card.pces.ToString() : "0";
            row.cell[2] = card.creationDate.HasValue ? card.creationDate.ToString() : "-";

            rows.Add(row);
        }

        return buildGrid(rows, headers, 1, 3);
    }

    private Grid.Result GetMembershipCardGrid()
    {
        var headers = new List<Grid.Header>
                          {
                              new Grid.Header() {label = "Card Name", width = 200},
                              new Grid.Header() {label = "Members", width = 70}
                          };

        var rows = new List<Grid.Row>();
        foreach (var card in _membershipCards)
        {
            var row = new Grid.Row
            {
                id = card.id,
                enabled = card.active.HasValue && (bool)card.active,
                cell = new string[2]
            };

            row.cell[0] = card.cardName;
            row.cell[1] = card.membersCount.HasValue ? card.membersCount.ToString() : "0";

            rows.Add(row);
        }

        return buildGrid(rows, headers, 1, 3);
    }
}

这是基础 GridVM

 public abstract class BaseGridVM
{
    protected static Grid.Result buildGrid(IEnumerable<Grid.Row> rows, IEnumerable<Grid.Header> headers, int page, int steps)
    {
        var row_array = rows.ToArray();
        var result = new Grid.Result
        {
            rows = row_array,
            page = page,
            records = row_array.Count(),
            steps = steps,
            headers = headers.ToArray()
        };
        return result;
    }
}

这是另一个不需要数据合同的类

公共类仪表板VM:IDashboardVM {

    public IResults_Dashboard results { get; private set; }
    public Grid.Result topCompaniesGrid
    {
        get { return BuildTopCompanies(); }
    }
    public Grid.Result topAdsGrid
    {
        get { return BuildTopAds(); }
    }

    public DashboardVM(IResults_Dashboard results)
    {
        this.results = results;
    }
    private static Grid.Result buildGrid(IEnumerable<Grid.Row> rows, IEnumerable<Grid.Header> headers, int page, int steps)
    {
        var row_array = rows.ToArray();
        var result = new Grid.Result
                         {
                             rows = row_array,
                             page = page,
                             records = row_array.Count(),
                             steps = steps,
                             headers = headers.ToArray()
                         };
        return result;
    }

    private Grid.Result BuildTopCompanies()
    {
        var headers = new List<Grid.Header>
                          {
                              new Grid.Header() {label = "Company Name", width = 150, click = true},
                              new Grid.Header() {label = "Coupon Views", width = 50, click = true},
                              new Grid.Header() {label = "Coupon Clicks", width = 50},
                              new Grid.Header() {label = "Coupon Redemptions", width = 50},
                              new Grid.Header() {label = "Ad Views", width = 50, click = true},
                              new Grid.Header() {label = "Ad Clicks", width = 50},
                              new Grid.Header() {label = "Reward Cards", width = 50},
                              new Grid.Header() {label = "Fees", width = 50}
                          };

        var rows = new List<Grid.Row>();
        foreach (var company in results.companies)
        {
            var row = new Grid.Row {id = Convert.ToInt32(company.companyId), cell = new string[8]};

            row.cell[0] = company.companyName;
            row.cell[1] = company.couponViews.ToString();
            row.cell[2] = company.couponClicks.ToString();
            row.cell[3] = company.couponRedemptions.ToString();
            row.cell[4] = company.adViews.ToString();
            row.cell[5] = company.adClicks.ToString();
            row.cell[6] = company.rewardCards.ToString();
            row.cell[7] = company.revenue.ToString();

            rows.Add(row);
        }

        return buildGrid(rows, headers, 1, 3);
    }

    private Grid.Result BuildTopAds()
    {
        var headers = new List<Grid.Header>
                          {
                              new Grid.Header() {label = "Company Name", width = 150, click = true},
                              new Grid.Header() {label = "Ad Name", width = 150, click = true},
                              new Grid.Header() {label = "Views", width = 50},
                              new Grid.Header() {label = "Clicks", width = 50},
                              new Grid.Header() {label = "Fees", width = 50}
                          };

        var rows = new List<Grid.Row>();
        foreach (var ad in results.ads)
        {
            var row = new Grid.Row {id = Convert.ToInt32(ad.Id), cell = new string[5]};

            row.cell[0] = ad.companyName;
            row.cell[1] = ad.name;
            row.cell[2] = ad.views.ToString();
            row.cell[3] = ad.clicks.ToString();
            row.cell[4] = ad.fees.ToString();

            rows.Add(row);
        }

        return buildGrid(rows, headers, 1, 3);
    }
}
4

1 回答 1

0

我通过添加无参数构造函数并向属性添加 set 方法解决了这个问题,而无需 Data Contract。

我从中收集到的是,序列化程序使用无参数构造函数创建一个新对象,然后将公共值复制过来。我这样说是因为当我没有将 set 方法添加到属性时,它给了我一个空对象。一旦我添加了 set 方法,值就会在对象中返回。

我认为它可以更复杂一点。我不知道为什么它会创建另一个对象而不是使用我给出的对象,但那是另一个话题!

于 2012-10-09T17:19:59.660 回答