Skip to content

Move System.Runtime.Serialization.Formatters.Binary.BinaryFormatter to CoreLib #39090

Closed
@marek-safar

Description

@marek-safar

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter type is always required by System.Private.CoreLib because it's used for resources serialization/deserialization. The required code after trimming in size sensitive scenarios ends up quite small (about 6kB) which indicates there is not a large chunk of code to be moved.

Moving the implementation would help reduce the number of file transfers for browser and get rid of extra reflection call in

private void InitializeBinaryFormatter()
{
LazyInitializer.EnsureInitialized(ref s_binaryFormatterType, () =>
Type.GetType("System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, System.Runtime.Serialization.Formatters, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
throwOnError: true)!);
LazyInitializer.EnsureInitialized(ref s_deserializeMethod, () =>
{
MethodInfo binaryFormatterDeserialize = s_binaryFormatterType!.GetMethod("Deserialize", new Type[] { typeof(Stream) })!;
// create an unbound delegate that can accept a BinaryFormatter instance as object
return (Func<object?, Stream, object>)typeof(ResourceReader)
.GetMethod(nameof(CreateUntypedDelegate), BindingFlags.NonPublic | BindingFlags.Static)!
.MakeGenericMethod(s_binaryFormatterType)
.Invoke(null, new object[] { binaryFormatterDeserialize })!;
});
_binaryFormatter = Activator.CreateInstance(s_binaryFormatterType!)!;
}

@eerhardt

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-wasmWebAssembly architecturearea-System.RuntimeenhancementProduct code improvement that does NOT require public API changes/additionslinkable-frameworkIssues associated with delivering a linker friendly framework

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions