簡體   English   中英

在c#中向對象動態添加屬性

[英]Dynamically adding properties to an object in c#

我有個問題:

public class PaginationSet
{
    public int TotalItemCount { get; set; }
    public int Page { get; set; }
    public int Amount { get; set; }
    public string Sort { get; set; }
    public string Order { get; set; }

    /// <summary>
    /// This is used to store all the above information in, while still maintaining the automated index count from the internal for loop link builder.
    /// 
    /// Don't forget to pass the index into this!
    /// </summary>
    public Func<int, object> PaginationLinkData
    {
        get
        {
            return index => new
            {
                page = index, // This is the internal pointer part that is used currently by Bootstrap pagination function
                amount = this.Amount,
                sort = this.Sort,
                order = this.Order
            };
        }
    }
}

this.Sortthis.Order有時是null 如果是,我想不要將它們放在返回的Func<int,object> ......我該怎么做呢?

它可能看起來像這樣:

    public Func<int, object> PaginationLinkData
    {
        get
        {
            Func<int,object> something = index => new
            {
                page = index, // This is the internal pointer part that is used currently by Bootstrap pagination function
                amount = this.Amount
            };

            if( this.Sort != null ) 
            {
                something.sort = this.Sort,
                something.order= this.Order
            }

            return something;
        }
    }
}

你不能這樣做嗎?

public Func<int, object> PaginationLinkData
    {
        get
        {
            if( this.Sort != null ) 
            {
                return index => new
                {
                    page = index, // This is the internal pointer part that is used currently by Bootstrap pagination function
                    amount = this.Amount,
                    sort = this.Sort,
                    order = this.Order
                };
            }
            else
            {
                return index => new
                {
                    page = index, // This is the internal pointer part that is used currently by Bootstrap pagination function
                    amount = this.Amount,
                };
            }
        }
    }

我猜你在某個地方序列化到JSon。 如果是這樣,你可以使用動態為什么不:

/// <summary>
/// This is used to store all the above information in, while still maintaining the automated index count from the internal for loop link builder.
/// 
/// Don't forget to pass the index into this!
/// </summary>
public Func<int, object> PaginationLinkData
{
    get
    {
        dynamic res = new ExpandoObject();

        res.amount = Amount;
        if (Sort != null) res.sort = Sort;
        if (Order != null) res.order = Order;

        return index =>
        {
            res.page = index;
            return res;
        };
    }
}

嘗試使用expando對象......

public Func<int, object> PaginationLinkData
{
    get
    {
        return index =>
            {
                dynamic obj = new ExpandoObject();
                obj.page = index;
                obj.amount = this.Amount;
                if (this.Sort != null)
                {
                    obj.sort = this.Sort;
                }
                if (this.Order != null)
                {
                    obj.order = this.Order;
                }
                return obj;
            };
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM