Skip to content

Commit e66452f

Browse files
alexjamesbrowncraiggwilson
authored andcommitted
added AllClassMaps - to enable us to get all the currently registered class maps out of BsonClassMap
1 parent 8d50296 commit e66452f

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Bson/Serialization/BsonClassMap.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ public static bool IsClassMapRegistered(Type type)
287287
}
288288
}
289289

290+
/// <summary>
291+
/// Returns all registered class maps.
292+
/// </summary>
293+
public static IEnumerable<BsonClassMap> AllClassMaps()
294+
{
295+
return __classMaps.Values;
296+
}
297+
290298
/// <summary>
291299
/// Looks up a class map (will AutoMap the class if no class map is registered).
292300
/// </summary>

BsonUnitTests/DefaultSerializer/BsonClassMapTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,48 @@ public void TestIsClassMapRegistered()
309309
}
310310
}
311311

312+
[TestFixture]
313+
public class BsonClassMapAllClassMapTests
314+
{
315+
private static bool __testAlreadyRan;
316+
317+
public class C
318+
{
319+
public ObjectId Id;
320+
public int X;
321+
}
322+
323+
public class D
324+
{
325+
public ObjectId Id;
326+
public string Y;
327+
}
328+
329+
[Test]
330+
public void TestAllClassMaps()
331+
{
332+
// test can only be run once
333+
if (__testAlreadyRan)
334+
{
335+
return;
336+
}
337+
338+
__testAlreadyRan = true;
339+
340+
Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(C)));
341+
Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(D)));
342+
BsonClassMap.RegisterClassMap<C>(cm => cm.AutoMap());
343+
BsonClassMap.RegisterClassMap<D>(cm => cm.AutoMap());
344+
345+
var classMaps = BsonClassMap.AllClassMaps();
346+
var classMapTypes = classMaps.Select(x => x.ClassType).ToList();
347+
348+
Assert.IsTrue(BsonClassMap.IsClassMapRegistered(typeof(C)));
349+
Assert.Contains(typeof(C), classMapTypes);
350+
Assert.Contains(typeof(D), classMapTypes);
351+
}
352+
}
353+
312354
[TestFixture]
313355
public class BsonShouldSerializeTests
314356
{

0 commit comments

Comments
 (0)