gpt4 book ai didi

c# - 声明 2 个相似的匿名对象

转载 作者:行者123 更新时间:2023-11-30 12:42:55 24 4
gpt4 key购买 nike

下面是2个匿名对象的声明。第二个与第一个完全相同,只是其中没有 lastRefresh 元素。

有没有办法在不复制完全相同的部分的情况下声明这 2 个对象?

var routeValuesForAutoRefresh = new
{
page = @ViewBag.nextPage,
lastRefresh = @ViewBag.lastRefresh,
searchTerm = Request["searchTerm"],
searchColumn = Request["searchColumn"],
searchOrder = Request["searchOrder"],
searchDescending = (Request["searchDescending"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus0 = (Request["requestStatus0"] ?? "false").ToLower().IndexOf("true") > -1,
requestStatus1 = (Request["requestStatus1"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus3 = (Request["requestStatus3"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus5 = (Request["requestStatus5"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus7 = (Request["requestStatus7"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus9 = (Request["requestStatus9"] ?? "true").ToLower().IndexOf("true") > -1
};
var routeValuesForNextPage = new
{
page = @ViewBag.nextPage,
searchTerm = Request["searchTerm"],
searchColumn = Request["searchColumn"],
searchOrder = Request["searchOrder"],
searchDescending = (Request["searchDescending"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus0 = (Request["requestStatus0"] ?? "false").ToLower().IndexOf("true") > -1,
requestStatus1 = (Request["requestStatus1"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus3 = (Request["requestStatus3"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus5 = (Request["requestStatus5"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus7 = (Request["requestStatus7"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus9 = (Request["requestStatus9"] ?? "true").ToLower().IndexOf("true") > -1
};

最佳答案

它没有语法,但你有几个不错的解决方法:

  • 命名你的类,并从另一个派生(或从一个公共(public)基础派生)
  • 使用公共(public)字段创建第三个匿名对象,然后在两个“真实”命名对象中使用它
  • 制作单独的局部变量,并使用它们制作两个匿名对象的字段。

我更喜欢第一种方法,但其他两种方法会有所改进。

这是第二种方法的例子:

var routeValuesForNextPage = new
{
page = @ViewBag.nextPage,
searchTerm = Request["searchTerm"],
searchColumn = Request["searchColumn"],
searchOrder = Request["searchOrder"],
searchDescending = (Request["searchDescending"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus0 = (Request["requestStatus0"] ?? "false").ToLower().IndexOf("true") > -1,
requestStatus1 = (Request["requestStatus1"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus3 = (Request["requestStatus3"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus5 = (Request["requestStatus5"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus7 = (Request["requestStatus7"] ?? "true").ToLower().IndexOf("true") > -1,
requestStatus9 = (Request["requestStatus9"] ?? "true").ToLower().IndexOf("true") > -1
};
var routeValuesForAutoRefresh = new {
routeValuesForNextPage
, lastRefresh = @ViewBag.lastRefresh
};

这是第三种方法的示例:

var page = @ViewBag.nextPage;
var searchTerm = Request["searchTerm"];
var searchColumn = Request["searchColumn"];
var searchOrder = Request["searchOrder"];
var searchDescending = (Request["searchDescending"] ?? "true").ToLower().IndexOf("true") > -1;
var requestStatus0 = (Request["requestStatus0"] ?? "false").ToLower().IndexOf("true") > -1;
var requestStatus1 = (Request["requestStatus1"] ?? "true").ToLower().IndexOf("true") > -1;
var requestStatus3 = (Request["requestStatus3"] ?? "true").ToLower().IndexOf("true") > -1;
var requestStatus5 = (Request["requestStatus5"] ?? "true").ToLower().IndexOf("true") > -1;
var requestStatus7 = (Request["requestStatus7"] ?? "true").ToLower().IndexOf("true") > -1;
var requestStatus9 = (Request["requestStatus9"] ?? "true").ToLower().IndexOf("true") > -1;

var routeValuesForAutoRefresh = new {
page,
lastRefresh = @ViewBag.lastRefresh,
searchTerm,
searchColumn,
searchOrder,
searchDescending,
requestStatus0,
requestStatus1,
requestStatus3,
requestStatus5,
requestStatus7,
requestStatus9
};
var routeValuesForNextPage = new {
page,
searchTerm,
searchColumn,
searchOrder,
searchDescending,
requestStatus0,
requestStatus1,
requestStatus3,
requestStatus5,
requestStatus7,
requestStatus9
};

关于c# - 声明 2 个相似的匿名对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32299512/

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