1

NEWBIE, Newbie, newbie question, sorry. I've just started programming in C# (coming long ago from the strongly typed world of c++). Its fun to be back programming but I see in the global.asax file the routes.MapRoute method that takes some kind of untyped object as the third and fourth parameter. Both instantiated the same, but possibly as two different types.

route.MapRoute( p1, p2, new { controller = "Home", Action = "Index" }, new {} );

Surprisingly, everything works well. But, I now want to access the 3rd parameter in a helper and it is coming in as an object type. I don't have any problem with casting the object type to whatever type of object it is, but I don't have a clue what it was instantiated as (nor can I find any docs that say more than object). Along the same line, how on earth does it know how to deal with something like 'controller' which exists inside the typed/untyped object? Now I have an unknown object type with unknown/varying property types inside. I would guess var?

Just as a comment on these assumptions made by the compiler, when I see examples of unit tests created just to make sure the loosely typed stuff is included and correctly spelled and typed, I lose confidence in the value of the loosely typed approach. Am I missing something?

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98

1 Answers1

2

the routes.MapRoute method that takes some kind of untyped object

It's not an untyped object; the object has a type, only it is an anonymous type.

how on earth does it know how to deal with something like 'controller' which exists inside the typed/untyped object?

It examines the object using reflection

But, I now want to access the 3rd parameter in a helper and it is coming in as an object type. I don't have any problem with casting the object type to whatever type of object it is, but I don't have a clue what it was instantiated as

The scope of anonymous types is limited to the method where they are used. If you need to reuse this type somewhere else, use a named type instead of an anonymous type.

Just to make it clear: the use of anonymous types doesn't "break" strong typing. It's just that in some cases you only need the type in one place, so you don't want to declare a type that you will only use once. In the case of the MapRoute method, the use of an anonymous type is just a convenience, as it's easier to declare an instance of an anonymous type than to manually create a dictionary (list of key/value pairs)

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Actually I'm not sure that reflection is used. Another discussion points out that anonymous types are generated by the compiler at compile time as compared to dynamics which are late-bound. – Shane Courtrille Dec 14 '11 at 16:48
  • @ShaneC, yes, they're generated by the compiler. It doesn't change the fact that the MapRoute method doesn't know the actual type (the parameter is declared as object), so the only way it can access its members is by using reflection. – Thomas Levesque Dec 14 '11 at 17:24
  • Wow, this is very fast response to an idiotic question. Thanks! I spent two hours looking and never noted "anonymous". Now that I've read a bit, I'll avoid it if possible. I just tried passing in a typed object derived from object with the correct parameters and it worked. phew! Thanks again! – user1056921 Dec 14 '11 at 18:47