Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit af64b49

Browse files
ViktorHoferdanmoseley
authored andcommitted
Using mscorlib shim as default resolving assembly for BinaryFormatter serialization instead of System.Private.Corelib (#20879)
* Using mscorlib shim as default resolving assembly instead of System.Private.Corelib * Increasing serialization perf * Doing string comparison on both mscorlib and S.P.C
1 parent 4c4a7b4 commit af64b49

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

src/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/FormatterServices.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,14 @@ internal static string GetClrAssemblyName(Type type, out bool hasTypeForwardedFr
322322
throw new ArgumentNullException(nameof(type));
323323
}
324324

325-
foreach (Attribute first in type.GetCustomAttributes(typeof(TypeForwardedFromAttribute), false))
325+
// Special case types like arrays
326+
Type attributedType = type;
327+
while (attributedType.HasElementType)
328+
{
329+
attributedType = attributedType.GetElementType();
330+
}
331+
332+
foreach (Attribute first in attributedType.GetCustomAttributes(typeof(TypeForwardedFromAttribute), false))
326333
{
327334
hasTypeForwardedFrom = true;
328335
return ((TypeForwardedFromAttribute)first).AssemblyFullName;

src/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ private long GetAssemblyId(WriteObjectInfo objectInfo)
984984
{
985985
assemId = 0;
986986
}
987-
else if (assemblyString.Equals(Converter.s_urtAssemblyString))
987+
else if (assemblyString.Equals(Converter.s_urtAssemblyString) || assemblyString.Equals(Converter.s_urtAlternativeAssemblyString))
988988
{
989989
// Urt type is an assemId of 0. No assemblyString needs
990990
// to be sent

src/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryTypeConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal static BinaryTypeEnum GetBinaryTypeInfo(Type type, WriteObjectInfo obje
5757
typeInformation = objectInfo.GetTypeFullName();
5858
}
5959

60-
if (assembly.Equals(Converter.s_urtAssemblyString))
60+
if (assembly.Equals(Converter.s_urtAssemblyString) || assembly.Equals(Converter.s_urtAlternativeAssemblyString))
6161
{
6262
binaryTypeEnum = BinaryTypeEnum.ObjectUrt;
6363
assemId = 0;

src/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/Converter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@ internal static class Converter
3030
internal static readonly Type s_typeofUInt64 = typeof(ulong);
3131
internal static readonly Type s_typeofObject = typeof(object);
3232
internal static readonly Type s_typeofSystemVoid = typeof(void);
33-
internal static readonly Assembly s_urtAssembly = s_typeofString.Assembly;
33+
34+
// In netfx the default assembly is mscorlib.dll --> typeof(string).Assembly.
35+
// In Core type string lives in System.Private.Corelib.dll which doesn't
36+
// contain all the types which are living in mscorlib in netfx. Therefore we
37+
// use our mscorlib facade which also contains manual type forwards for deserialization.
38+
internal static readonly Assembly s_urtAssembly = Assembly.Load("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
3439
internal static readonly string s_urtAssemblyString = s_urtAssembly.FullName;
3540

41+
internal static readonly Assembly s_urtAlternativeAssembly = s_typeofString.Assembly;
42+
internal static readonly string s_urtAlternativeAssemblyString = s_urtAlternativeAssembly.FullName;
43+
3644
// Arrays
3745
internal static readonly Type s_typeofTypeArray = typeof(Type[]);
3846
internal static readonly Type s_typeofObjectArray = typeof(object[]);

0 commit comments

Comments
 (0)