Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialize custom properties on a class that implements IEnumerable
    text
    copied!<p>Currently, JSON.NET ignores all other properties on classes implementing IEnumerable and serializes the array.</p> <p>How can I tell JSON.NET to serialize the custom properties? I'm trying to serialize the <code>PagedList&lt;T&gt;</code> implementation below:</p> <pre><code>public interface IPagedList : IEnumerable { int PageIndex { get; } int PageSize { get; } int TotalCount { get; } int TotalPages { get; } bool HasPreviousPage { get; } bool HasNextPage { get; } } public interface IPagedList&lt;T&gt; : IPagedList, IList&lt;T&gt; { } /// &lt;summary&gt; /// A tried and tested PagedList implementation /// &lt;/summary&gt; public class PagedList&lt;T&gt; : List&lt;T&gt;, IPagedList&lt;T&gt; { public PagedList(IEnumerable&lt;T&gt; source, int pageIndex, int pageSize) : this(source.GetPage(pageIndex, pageSize), pageIndex, pageSize, source.Count()) { } public PagedList(IEnumerable&lt;T&gt; source, int pageIndex, int pageSize, int totalCount) { Ensure.Argument.NotNull(source, "source"); this.TotalCount = totalCount; this.TotalPages = totalCount / pageSize; if (totalCount % pageSize &gt; 0) TotalPages++; this.PageSize = pageSize; this.PageIndex = pageIndex; this.AddRange(source.ToList()); } public int PageIndex { get; private set; } public int PageSize { get; private set; } public int TotalCount { get; private set; } public int TotalPages { get; private set; } public bool HasPreviousPage { get { return (PageIndex &gt; 0); } } public bool HasNextPage { get { return (PageIndex + 1 &lt; TotalPages); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload