在c#中向对象动态添加属性

时间:2015-03-09 22:45:51

标签: c# object func

我遇到了问题:

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

3 个答案:

答案 0 :(得分:1)

你不能做这样的事吗?

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

答案 1 :(得分:1)

我猜你正在某个地方序列化到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;
        };
    }
}

答案 2 :(得分:1)

尝试使用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;
            };
    }
}