Picture below shows a MSDN reference of RouteCollectionExtensions.Maproute
method.
The method have 1 parameter called defaults which is an object type.
Picture below shows a line of code (boxed) in a ASP.NET MVC project in Visual Studio which don't contain errors.
The code create a new object and passes it as argument into MapRoute
method.
The object contains members namely controller
, action
and id
.
However, the object is created from System.Object
class. There is no such members specified in the System.Object class.
How the object be created since it do not have the class that have these members?
There are various separate ideas you need to understand here.
Most importantly, there's the idea of an anonymous type. Look at this:
var x = new { Name = "Jon", Location = "Reading" };
That has created an instance of an anonymous type, and assigned the resulting reference to the variable x
. At compile-time, the compiler will generate a type that actually has a name, but one you can't refer to.
Now in this case, x
will be strongly typed to that "unnamed" type - but it would also be fine to use:
object x = new { Name = "Jon", Location = "Reading" };
After all, we're just creating an object, and every class type is compatible with object
, in the same way that we could write:
object x = new StringBuilder();
The example code you've given is very similar, except that it's using the value as an argument to a method call, instead of assigning it to a variable.
The MapRoute
method uses reflection to determine the properties of the anonymous type and the values stored in the instance passed in.
See more on this question at Stackoverflow