On Sunday, August 26, 2012 10:41:13 PM UTC+2, Stan.Konce wrote:
> Thanks a lot Robert,
> I will need a little time to look into it, eventually tomorrow (it's
> already night here).
> But I am really very thankful!
> Stan
> On 2012-08-26 22:32, Robert Stam wrote:
> Version 1.2 of the C# driver is really old. The current version is 1.5.
> Here's a working program (using version 1.5 of the driver) based on your
> sample classes:
> http://pastie.org/4593182
> A few notes:
> You can't use serialization attributes on interfaces or their members.
> They only work on classes and their members.
> Since you can't use the [BsonKnownTypes] attribute you have to manually
> ensure that the derived classes are known to the driver. The easiest way to
> do that is to call LookupSerializer, like this:
> BsonSerializer.LookupSerializer(typeof(Person));
> BsonSerializer.LookupSerializer(typeof(Organization));
> You would do that once as the program is starting up.
> On Sun, Aug 26, 2012 at 3:29 PM, Stane <stan....@googlemail.com<javascript:>
> > wrote:
>> Hi,
>> it's me again with a new question. I am trying to serialize polymorphic
>> collections where the root type is an interface (which is quite
>> indispensable for my purposes).
>> In a follow-up to a discussion thread a year ago (e.g.
>> http://osdir.com/ml/mongodb-user/2011-06/msg01561.html) there was an
>> indication that the interface support would be included into version 1.2. I
>> am using version 1.2.0.4274, but I do not have an impression that it is
>> really included.
>> I am trying to serialize/deserialize collections that are declared
>> roughly as follows (simplified):
>> [BsonKnownTypes(typeof(Person), typeof(Organization))]
>> [BsonDiscriminator(RootClass = true)]
>> public interface ICodedValue
>> {
>> [BsonId]
>> public string Code {get;set;}
>> public string Name {get;set;}
>> }
>> public class Person : ICodedValue
>> {
>> [BsonId]
>> public string Code {get;set;}
>> public string Name {get;set;}
>> public Gender Gender {get;set;}
>> public DateTime BirthDate {get;set;}
>> }
>> public class Organization : ICodedValue
>> {
>> [BsonId]
>> public string Code {get;set;}
>> public string Name {get;set;}
>> public Address Address {get;set;}
>> public string Phone {get;set;}
>> }
>> (As far as I understand, this is equivalent to the declarations via
>> BsonClassMap.RegisterClassMap() which I also tried with no success).
>> The interface so declared won't even compile:
>> "Attribute 'BsonKnownTypes' is not valid on this declaration type. It is
>> only valid on 'class, struct' declarations."
>> "Attribute 'BsonDiscriminator' is not valid on this declaration type. It
>> is only valid on 'class, struct' declarations."
>> An attempt to use an abstract class is obviously no solution, since no
>> instances can be initialized. If I declare a normal class as the root type
>> (like in the "Animal-Cat-Tiger" example in the tutorial) everything
>> functions fine. But I need interfaces.
>> Are interfaces not supported in the end or am I doing things wrongly?
>> In the best case i would appreciate a minimal code example with an
>> interface as root type that functions.
>> Thanks in advance,
>> Stan