Skip to content

Commit 53073cb

Browse files
KonHrstam
authored andcommitted
CSHARP-2595: Fix initialization on .NET Core 3.0 preview 4.
This change cover .NET Framework and .NET Core ways to get System.Runtime.Serialization.FormatterServices type instance.
1 parent 6d9b44d commit 53073cb

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/MongoDB.Bson/Serialization/BsonClassMap.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ public class BsonClassMap
3838
private readonly static Queue<Type> __knownTypesQueue = new Queue<Type>();
3939

4040
private static readonly MethodInfo __getUninitializedObjectMethodInfo =
41-
typeof(string)
42-
.GetTypeInfo()
43-
.Assembly
44-
.GetType("System.Runtime.Serialization.FormatterServices")
41+
GetFormatterServicesType()
4542
.GetTypeInfo()
4643
?.GetMethod("GetUninitializedObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
4744

@@ -1306,7 +1303,21 @@ private void ThrowNotFrozenException()
13061303
var message = string.Format("Class map for {0} has been not been frozen yet.", _classType.FullName);
13071304
throw new InvalidOperationException(message);
13081305
}
1309-
}
1306+
1307+
private static Type GetFormatterServicesType()
1308+
{
1309+
#if NET452
1310+
return typeof(string)
1311+
.GetTypeInfo()
1312+
.Assembly
1313+
.GetType("System.Runtime.Serialization.FormatterServices");
1314+
#else
1315+
return
1316+
Assembly.Load(new AssemblyName("System.Runtime.Serialization.Formatters"))
1317+
.GetType("System.Runtime.Serialization.FormatterServices");
1318+
#endif
1319+
}
1320+
}
13101321

13111322
/// <summary>
13121323
/// Represents a mapping between a class and a BSON document.

0 commit comments

Comments
 (0)