gpt4 book ai didi

c# - c#动态添加对象属性

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:16 26 4
gpt4 key购买 nike

我有一个问题:

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

最佳答案

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

关于c# - c#动态添加对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com