0

私は.NETWebAPIを使用しており、もちろんJSONにシリアル化されたクラスを返します。これまで、どのクラスにもデータコントラクト属性を使用する必要はありませんでしたが、以下のこのクラスでは使用するので、理由がわかりません。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);
    }
}

これはBaseGridVMです

 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;
    }
}

そして、これはデータコントラクトが要求されない別のクラスです

パブリッククラスDashboardVM: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メソッドを追加することで、データコントラクトなしでこの問題を解決しました。

これから私が集めたのは、シリアライザーがパラメーターなしのコンストラクターを使用して新しいオブジェクトを作成し、パブリック値をコピーすることです。これは、setメソッドをプロパティに追加しなかったときに、空のオブジェクトが表示されたためです。setメソッドを追加すると、オブジェクト内に値が返されました。

もう少し洗練されていると思います。なぜそれが別のオブジェクトを作成するのか、私が与えたオブジェクトを使用するのかはわかりませんが、それは別のトピックです!

于 2012-10-09T17:19:59.660 に答える