BsonClassMap.RegisterClassMap for runtime types

806 views
Skip to first unread message

Omega Red

unread,
Feb 7, 2012, 2:14:57 PM2/7/12
to mongodb...@googlegroups.com
I have one base Component class (which acts basically like an interface) and many classes that derive from it. In a collection I store elements that can contain arbitrary number of those derived Component objects. Now to properly deserialize such elements I need to call BsonClassMap.RegisterClassMap() for each of the derived types, otherwise deserializer complains about members not being mappable to base Component class. I'd like to use reflection to enumerate all Component-derived types and register class maps for them at the beginning, but there is a problem: it appears that there is no RegisterClassMap overload that takes a Type argument, only generic versions. Sticking a call to RegisterClassMap into static constructors don't help since their invocation order is not guaranteed. Any ideas how to achieve such run-time initialization without knowing the types beforehand?

Robert Stam

unread,
Feb 7, 2012, 2:18:51 PM2/7/12
to mongodb...@googlegroups.com
You can checkout this similar question:


You can call LookupClassMap instead of RegisterClassMap if you don't need to customize the class map in any way. LookupClassMap automaps any classes that don't have a class map yet, so calling LookupClassMap results in a class map being registered if necessary.

Omega Red

unread,
Feb 7, 2012, 2:36:03 PM2/7/12
to mongodb...@googlegroups.com
Excellent, that works fine. My search-fu was week today...
What if I need to customize the class map? Can I change the one returned by LookupClassMap()?

Robert Stam

unread,
Feb 7, 2012, 2:39:13 PM2/7/12
to mongodb...@googlegroups.com
If you need to customize it then you need to call RegisterClassMap.

LookupClassMap returns the one you registered with RegisterClassMap, but if it can't find one then it automatically registers an automapped class map for the class.
Reply all
Reply to author
Forward
0 new messages