Using Reflection (and O/R Mappers) with Nested Classes

I can't say that I've ever used a nested class myself, but I recently had a problem posted to my forums that involved a nested class and my ORMapper.  I tested it myself and concluded that it was a bug due to my never having worrying about it before.  Well it wasn't easy to find the solution, but I'm happy to report that there was no bug at all -- you just have to know the trick that .NET expects when using nested classes with reflection.  It turns out that you should use a plus sign (+) instead of a dot (.) when referring to the nested class in a type name that you are going to reflect on -- so something like this:  "Name.Space.Main+Nested".  Its even in the MSDN docs here, although its not in other parts of the MSDN docs that I was originally looking for it at, like here.  Anyhow, I assume this information will be useful to anyone using reflection with nested classes, and it probably applies to other O/R Mappers also, so it seemed worthy of posting on this blog.
Published Monday, April 10, 2006 10:51 AM by PaulWilson

Comments

# re: Using Reflection (and O/R Mappers) with Nested Classes

I gotten bitten by it recently because I didn't make the difference between the way C# handles it and the way the framework API does it.

I'm not sure why the distinction is important, though.

Monday, April 10, 2006 5:14 PM by Ayende Rahien

# re: Using Reflection (and O/R Mappers) with Nested Classes

man, I was about to pull my hair out..

thanks a lot for the tip, you saved me hours

Monday, April 23, 2007 1:46 PM by Marco

Leave a Comment

(required) 
(required) 
(optional)
(required)