mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 03:39:35 -10:00
This ports BinaryFormatter from desktop, along with all of the supporting types and general Formatter-related types. I've added a bunch of tests, bringing code coverage up to ~80%, though there's still more that can/should be done. I did not port remoting-specific functionality, though there are still some remnants I've left as they were enabled in the runtime even when remoting wasn't compiled in, and it wasn't clear if they were serving additional purposes. Commit migrated from https://github.com/dotnet/corefx/commit/e45a01a4840700d75bb1dd3bf7d0e10ff6a3bc82
53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using Xunit;
|
|
|
|
namespace System.Runtime.Serialization.Formatters.Tests
|
|
{
|
|
public class SerializationBinderTests
|
|
{
|
|
[Fact]
|
|
public void BindToName_NullDefaults()
|
|
{
|
|
var b = new TrackAllBindToTypes();
|
|
string assemblyName, typeName;
|
|
b.BindToName(typeof(string), out assemblyName, out typeName);
|
|
Assert.Null(assemblyName);
|
|
Assert.Null(typeName);
|
|
}
|
|
|
|
[Fact]
|
|
public void BindToType_AllValuesTracked()
|
|
{
|
|
var s = new MemoryStream();
|
|
var f = new BinaryFormatter();
|
|
|
|
f.Serialize(s, DayOfWeek.Monday);
|
|
s.Position = 0;
|
|
|
|
var t = new TrackAllBindToTypes();
|
|
f.Binder = t;
|
|
f.Deserialize(s);
|
|
|
|
Assert.Contains(t.Binds, kvp => kvp.Value.Contains("System.DayOfWeek"));
|
|
}
|
|
|
|
private class TrackAllBindToTypes : SerializationBinder
|
|
{
|
|
public readonly List<KeyValuePair<string, string>> Binds = new List<KeyValuePair<string, string>>();
|
|
|
|
public override Type BindToType(string assemblyName, string typeName)
|
|
{
|
|
Binds.Add(new KeyValuePair<string, string>(assemblyName, typeName));
|
|
return Assembly.Load(new AssemblyName(assemblyName)).GetType(typeName);
|
|
}
|
|
}
|
|
}
|
|
}
|