Skip to content

Instantly share code, notes, and snippets.

@andreabalducci
Created September 30, 2014 08:56
Show Gist options
  • Save andreabalducci/5c397cce09bd16e14f09 to your computer and use it in GitHub Desktop.
Save andreabalducci/5c397cce09bd16e14f09 to your computer and use it in GitHub Desktop.
Reset class map for unit testing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MongoDB.Bson.Serialization;
namespace CQRS.Tests.DomainTests
{
public static class BsonClassMapHelper
{
public static void Unregister<T>()
{
var classType = typeof(T);
GetClassMap().Remove(classType);
}
static Dictionary<Type, BsonClassMap> GetClassMap()
{
var cm = BsonClassMap.GetRegisteredClassMaps().First();
var fi = typeof(BsonClassMap).GetField("__classMaps", BindingFlags.Static | BindingFlags.NonPublic);
var classMaps = (Dictionary<Type, BsonClassMap>)fi.GetValue(cm);
return classMaps;
}
public static void Clear()
{
GetClassMap().Clear();
}
}
}
@andreabalducci
Copy link
Author

mongodb c# driver 1.9.2

@ckuetbach
Copy link

Looks like the current driver is still comatible to this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment