简体   繁体   English

VB.NET / ASP.NET With和New关键字混乱

[英]VB.NET/ASP.NET With and New keyword muddle

Can someone explain how New works with the With keyword in this example from the MVC framework. 有人可以从MVC框架解释New如何With此示例中的With关键字。

routes.MapRoute("Default", 
                "{controller}/{action}/{id}", 
                New With {.controller = "Home", .action = "Index", .id = ""})

This syntax is used to create an anonymous type in VB.Net. 此语法用于在VB.Net中创建匿名类型。

It allows you to define a type on the fly with a set of name / value pairs. 它允许您使用一组名称/值对动态定义类型。 The names all turn into properties on the type. 名称都变成了类型的属性。 If you open up the generated assembly in reflector you will be able to see these types. 如果在反射器中打开生成的组件,您将能够看到这些类型。

Don't let the With syntax portion fool you. 不要让With语法部分欺骗你。 This feature has nothing to do with the "With" context feature of VB.Net. 此功能与VB.Net的“With”上下文功能无关。 Other than the "." 除了“。” prefix on the value names. 值名称上的前缀。

It creates a new anonymous class with the specified properties. 它创建一个具有指定属性的匿名类。

More information from MSDN . 来自MSDN的更多信息。

Here "new" is declaring an anonymous class. 这里“新”宣布一个匿名类。

This class has no formal definition (ie no "Public Class " definition), it's structure is defined by the items in the curly's after the with. 这个类没有正式的定义(即没有“公共类”定义),它的结构是由with之后的卷曲中的项定义的。 So it defines and constructs a class in a single statement. 所以它在一个语句中定义和构造一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM