Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Mongo Bson Serialization of null Interface properties.

By: zippy1981 on Sep 23rd, 2012  |  syntax: C#  |  size: 1.26 KB  |  views: 47  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. using MongoDB.Bson;
  5. using MongoDB.Bson.Serialization;
  6. using MongoDB.Driver;
  7.  
  8. namespace ConsoleApplication2
  9. {
  10.     public interface IProp
  11.     {
  12.         string AString { get; set; }
  13.     }
  14.  
  15.     public class PropImpl:IProp
  16.     {
  17.         public string AString { get; set; }
  18.     }
  19.  
  20.     public class SuperClass
  21.     {
  22.         public string Name { get; set; }
  23.         public IProp AProp { get; set; }
  24.     }
  25.  
  26.     class Program
  27.     {
  28.         private static MongoServer server = MongoServer.Create("mongodb://localhost/?safe=true");
  29.         private static MongoDatabase db = server["InterfaceTest"];
  30.         static void Main()
  31.         {
  32.             var superClass1 = new SuperClass
  33.                 {
  34.                     Name = Guid.NewGuid().ToString(),
  35.                     AProp = new PropImpl()
  36.                 };
  37.             var superClass2 = new SuperClass
  38.             {
  39.                 Name = Guid.NewGuid().ToString()
  40.             };
  41.             BsonSerializer.LookupSerializer(typeof(PropImpl));
  42.             Debug.Print("superClass1: {0}", superClass1.ToJson());
  43.             Debug.Print("superClass2: {0}", superClass2.ToJson());
  44.             //var collection = db["PropA"].Insert(superClass);
  45.         }
  46.     }
  47. }
clone this paste RAW Paste Data