Generics in OJB.Net (a.k.a. Generics Type Madness)


At iP3 we are using OJB.Net for object relational persistence. It is really easy to use, but it was using an ArrayList by default and we wanted to use the new Whidbey Generics List<> type.


When OJB.Net creates objects out of the relational data, it uses reflection to generate the IList collection object which is ArrayList by default.


When we tried to change this to the Generics List<> type OJB.Net didn’t seem to be able to handle it and reported that it couldn’t find the type in the assembly. My collegue Lance and I sat and worked through the problem for about half an hour until he did a GetType().ToString() on a List<> object. To our surprise we found that the type name we needed to Reflect for was System.Collections.Generic.List’1 linstead of System.Collections.Generic.List.


The full line for getting the type for List<String> using reflection then becomes:


Type classType = Type.GetType(“System.Collections.Generic.List’1[[System.String,Mscorlib]],Mscorlib“,true); 


Don Box has a good post about Generics reflection under the heading Generics as Type Constructors from August 2003, but it doesn’t mention anything about adding a suffix to the type name.


If someone has seen this suffix notation explained previously then please shoot me a link.


Read more about the craziness on Lance’s blog.

Comments (5)

  1. This is so you can overload based on the number of type parameters. E.g. you could define a System.Collections.List<T,U> type if you wanted because in the metadata it would have a different name (…List2 instead of ...List1). An alternative design to this name mangling would be to make everyone aware that you can overload on number of type parameters and just call everything ‘List’, but the name mangling is sometimes simpler and more convenient.

  2. Chris Garty says:

    Thanks Dominic. The "’x suffix where x is the number of generic types" reason was something Lance had speculated about afterwards. We couldn’t find documentation about this anywhere, so hopefully someone will benefit from our posts.

  3. secretGeek says:

    Hi Chris

    I noticed you dropped by my blog in the last 24 hours, possibly referred by someone else via email, in relation to .net naming conventions and the use of underscore.

    Please don’t let my blog influence your use of underscores in code.

    Contrary to what it says on the page you found (http://www.secretgeek.net/hungarian.asp)

    I actually do not recommend the use of underscores in dot net code ever.

    cheers

    Leon

    p.s. actually i just left this message in order to freak you out about the fact that the ip3 systems exchange server reveals so much about your identity when you are simply browsing!

  4. jaybaz [MS] says:

    Sounds like you got it figured out. The number is the count of generic type parameters. The reason for this name mangling is to support overloading of types based on generic type arity. The delimiter is backtick, which is on the same key as tilde on US keyboards.