下面是2个匿名对象的声明。第二个是第一个完全相同的副本,除了里面没有lastRefresh
元素。
有没有办法声明这两个对象而不重复完全相同的部分?
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
};
答案 0 :(得分: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
};
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
};
答案 1 :(得分:0)
我的问题:有没有办法声明这两个对象没有 复制完全相同的部分?
当然,使用常规类。
实现一个应该包含共享属性的类RouteValues
,并实现继承RouteValues
以添加更多属性的派生类。
如果您担心必须使用点语法(即values.whatever = "hello world";
)逐个设置每个属性,则可以使用 type initializers :
RouteValues values = new RouteValues
{
page = @ViewBag.nextPage
};