c# - Serialize Hashset of enum in MongoDB -


i want serialize in mongodb, member engines (hashset of enum) string see name string , not number class :

public class enginesperfile {     public guid fileid { get; set; }     public string path { get; set; }     public hashset<enginetype> engines { get; set; } } 

i trying map :

bsonclassmap.registerclassmap<enginesperfile>(cm =>             {                 cm.automap();                 cm.mapidfield(c => c.fileid);                 cm.mapmember(c => c.engines).setserializer(new enumserializer<enginetype>(bsontype.string));             }); 

but error:

value type of serializer enginetype , not match member type system.collections.generic.hashset`1[[playground.model.enginetype, model, version=1.0.0.0, culture=neutral, publickeytoken=null]]. 

parameter name: serializer.

how can serialize hashset of enum in mongodb?

i fix writing custom serializer: (enginetype can generic t)

 public class myhashsetserializer : serializerbase<hashset<enginetype>> {     private readonly ibsonserializer _serializer =         bsonserializer.lookupserializer(typeof(enginetype));      public override void serialize(bsonserializationcontext context, bsonserializationargs args, hashset<enginetype> value)     {         var set = (hashset<enginetype>)value;          context.writer.writestartdocument();                    int = 0;         foreach (var element in set)         {              context.writer.writename($"[{i}]");             context.writer.writestring(element.tostring());         }         context.writer.writeenddocument();     }      public override hashset<enginetype> deserialize(bsondeserializationcontext context, bsondeserializationargs args)     {         var set = new hashset<enginetype>();          context.reader.readstartdocument();         while (context.reader.readbsontype() != 0)         {             context.reader.skipname();             var element =                 (enginetype)_serializer.deserialize(context, args);                           set.add(element);         }         context.reader.readenddocument();          return set;     } } 

and register class map this:

bsonclassmap.registerclassmap<enginesperfile>(cm =>             {                 cm.automap();                 cm.mapidfield(c => c.fileid);                 cm.mapmember(c => c.engines).setserializer(new myhashsetserializer());             }); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -