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

C# 在c语言中向对象动态添加属性#,c#,object,func,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>

我有一个问题:

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

你就不能这样做吗

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,
                };
            }
        }
    }
public Func PaginationLinkData
{
得到
{
if(this.Sort!=null)
{
返回索引=>new
{
page=index,//这是引导分页函数当前使用的内部指针部分
金额=这个。金额,
sort=this.sort,
订单=这个。订单
};
}
其他的
{
返回索引=>new
{
page=index,//这是引导分页函数当前使用的内部指针部分
金额=这个。金额,
};
}
}
}

我猜您正在某个地方序列化到JSon。如果是,您可以使用dynamic why not:

/// <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;
        };
    }
}
//
///这用于将上述所有信息存储在中,同时仍保持来自内部for循环链接生成器的自动索引计数。
/// 
///别忘了把索引传给这个!
/// 
公共函数分页链接数据
{
得到
{
dynamic res=新的ExpandooObject();
res.amount=金额;
如果(Sort!=null)res.Sort=Sort;
如果(Order!=null)res.Order=Order;
返回索引=>
{
res.page=索引;
返回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;
            };
    }
}
public Func PaginationLinkData
{
得到
{
返回索引=>
{
动态对象=新的ExpandooObject();
obj.page=索引;
obj.amount=该金额;
if(this.Sort!=null)
{
obj.sort=this.sort;
}
if(this.Order!=null)
{
obj.order=此.order;
}
返回obj;
};
}
}

我想你可以在这个问题上使用
表达式
动态编译一个lambda。我将尝试给你一个可能对你有帮助的链接:Lol。嗯,我可以。。。英雄联盟只是想换上一些很酷的墨镜。
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;
            };
    }
}