From a8c517f11fd4d4bbd2054cd659b923fef4640464 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Fri, 8 Apr 2022 20:08:57 +0700 Subject: [PATCH 1/2] Remove netstandard1.6 platform target, update NETFX target to net471 --- src/Hyperion.Benchmarks/Prolog.cs | 6 +- .../SerializeClassesBenchmark.cs | 3 +- src/Hyperion.Tests/Bugs.cs | 25 ++++-- src/Hyperion.Tests/CollectionTests.cs | 18 +++-- src/Hyperion.Tests/CustomObjectTests.cs | 2 +- src/Hyperion.Tests/CyclicTests.cs | 18 +++-- src/Hyperion.Tests/DelegateTests.cs | 8 +- src/Hyperion.Tests/ExpressionTests.cs | 2 +- src/Hyperion.Tests/IlCompilerTests.cs | 18 ++--- src/Hyperion.Tests/InterfaceTests.cs | 2 +- src/Hyperion.Tests/SurrogateTests.cs | 6 +- .../UnsupportedTypeSerializerTests.cs | 3 - src/Hyperion/Compilation/IlBuilder.cs | 2 - src/Hyperion/Compilation/IlCompiler.cs | 2 - src/Hyperion/Compilation/IlCompilerContext.cs | 2 - src/Hyperion/Compilation/IlExpression.cs | 2 - src/Hyperion/Extensions/TypeEx.cs | 34 -------- src/Hyperion/Hyperion.csproj | 25 ++---- src/Hyperion/Serializer.cs | 2 +- .../AggregateExceptionSerializerFactory.cs | 17 ---- .../ExceptionSerializerFactory.cs | 19 +---- src/Hyperion/SerializerOptions.cs | 81 +++++++++++-------- ...nAwareByteArrayRequiringValueSerializer.cs | 4 +- .../SessionIgnorantValueSerializer.cs | 4 +- 24 files changed, 127 insertions(+), 178 deletions(-) diff --git a/src/Hyperion.Benchmarks/Prolog.cs b/src/Hyperion.Benchmarks/Prolog.cs index 224ba5ce..ff2dbd38 100644 --- a/src/Hyperion.Benchmarks/Prolog.cs +++ b/src/Hyperion.Benchmarks/Prolog.cs @@ -21,9 +21,9 @@ public class HyperionConfig : ManualConfig { public HyperionConfig() { - Add(StatisticColumn.Mean, StatisticColumn.Min, StatisticColumn.Max, StatisticColumn.OperationsPerSecond); - Add(MarkdownExporter.GitHub); - Add(MemoryDiagnoser.Default); + AddColumn(StatisticColumn.Mean, StatisticColumn.Min, StatisticColumn.Max, StatisticColumn.OperationsPerSecond); + AddExporter(MarkdownExporter.GitHub); + AddDiagnoser(MemoryDiagnoser.Default); } } diff --git a/src/Hyperion.Benchmarks/SerializeClassesBenchmark.cs b/src/Hyperion.Benchmarks/SerializeClassesBenchmark.cs index 35203e22..15e43790 100644 --- a/src/Hyperion.Benchmarks/SerializeClassesBenchmark.cs +++ b/src/Hyperion.Benchmarks/SerializeClassesBenchmark.cs @@ -25,7 +25,8 @@ public class SerializeClassesBenchmark : HyperionBenchmark protected override void Init() { - var baseOptions = new SerializerOptions(preserveObjectReferences: true); + var baseOptions = SerializerOptions.Default + .WithPreserveObjectReferences(true); Serializer = new Serializer(baseOptions); var filteredOptions = baseOptions diff --git a/src/Hyperion.Tests/Bugs.cs b/src/Hyperion.Tests/Bugs.cs index cfa3342c..a6e243f5 100644 --- a/src/Hyperion.Tests/Bugs.cs +++ b/src/Hyperion.Tests/Bugs.cs @@ -95,7 +95,9 @@ public void CanSerializeMessageWithByte() { var stream = new MemoryStream(); var msg = new ByteMessage(DateTime.UtcNow, 1, 2); - var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); serializer.Serialize(msg, stream); stream.Position = 0; var res = serializer.Deserialize(stream); @@ -107,7 +109,9 @@ public void CanSerializeMessageWithByte() [Fact] public void CanFindTypeByManifest_WhenManifestContainsUnknownAssemblyVersion() { - var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); var type = typeof(ByteMessage); MemoryStream GetStreamForManifest(string manifest) @@ -168,7 +172,9 @@ public void TypeEx_ToQualifiedAssemblyName_should_strip_version_correctly_for_mu public void CanSerialieCustomType_bug() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); var root = new Recover(SnapshotSelectionCriteria.Latest); serializer.Serialize(root, stream); @@ -179,7 +185,9 @@ public void CanSerialieCustomType_bug() [Fact] public void CanSerializeImmutableGenericInterfaces() { - var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); var names = new List> { new Container("Mr", TrustLevel.Partial), @@ -210,7 +218,9 @@ public void CanSerializeUri() { var stream = new MemoryStream(); var msg = new Uri("http://localhost:9202/", UriKind.RelativeOrAbsolute); - var serializer = new Serializer(new SerializerOptions(preserveObjectReferences: true, versionTolerance: true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); serializer.Serialize(msg, stream); stream.Position = 0; var res = serializer.Deserialize(stream); @@ -458,7 +468,7 @@ public void WritesManifestEvenIfKnown() { 42, "iMaGiNe" } }.ToImmutableDictionary(), }; - var serializer = new Serializer(new SerializerOptions(knownTypes: new[] + var serializer = new Serializer(SerializerOptions.Default.WithKnownTypes(new[] { typeof(object[]), typeof(int[]), @@ -494,8 +504,11 @@ public void WritesManifestEvenIfKnown() class FieldsToOrder { +#pragma warning disable CS0649 public string A2; + // ReSharper disable once InconsistentNaming public string a1; +#pragma warning restore CS0649 } [Fact] diff --git a/src/Hyperion.Tests/CollectionTests.cs b/src/Hyperion.Tests/CollectionTests.cs index 91ecb9de..fbf1a041 100644 --- a/src/Hyperion.Tests/CollectionTests.cs +++ b/src/Hyperion.Tests/CollectionTests.cs @@ -398,7 +398,9 @@ public void CanSerializeArray3DOfInt() public void Issue18() { var msg = new byte[] {1, 2, 3, 4}; - var serializer = new Serializer(new SerializerOptions(true, true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); byte[] serialized; using (var ms = new MemoryStream()) @@ -420,9 +422,9 @@ public void Issue18() public void Issue263_CanSerializeArrayOfSurrogate_WhenPreservingObjectReference() { var invoked = new List(); - var serializer = new Serializer(new SerializerOptions( - preserveObjectReferences: true, - surrogates: new [] + var serializer = new Serializer(SerializerOptions.Default + .WithPreserveObjectReferences(true) + .WithSurrogates(new [] { Surrogate.Create( to => to.ToSurrogate(), @@ -562,7 +564,9 @@ public class NotKnown { } public abstract class BaseContainer : IList, ICollection, IEnumerable, IEnumerable, IList, ICollection { +#pragma warning disable CS0649 private List _inner; +#pragma warning restore CS0649 #region interfaces methods @@ -663,9 +667,9 @@ public void Add(NotKnown item) [Fact] public void CanInstantiateSerializerForCollectionWithAmbiguousAddMethod() { - var serializer = new Serializer( - new SerializerOptions(knownTypes: new List {typeof(DerivedContainer)}, - serializerFactories: new List {new DerivedContainerSerializerFactory()})); + var serializer = new Serializer(SerializerOptions.Default + .WithKnownTypes(new List {typeof(DerivedContainer)}) + .WithSerializerFactory(new List {new DerivedContainerSerializerFactory()})); var init = new DerivedContainer(); serializer.Serialize(init, new MemoryStream()); // we're done if AmbiguousMatchException wasn't fired diff --git a/src/Hyperion.Tests/CustomObjectTests.cs b/src/Hyperion.Tests/CustomObjectTests.cs index 4a87e643..ed766f88 100644 --- a/src/Hyperion.Tests/CustomObjectTests.cs +++ b/src/Hyperion.Tests/CustomObjectTests.cs @@ -222,7 +222,7 @@ public void CanEmptyObject() [Fact] public void CanSerializeObjectsKnownTypes() { - CustomInit(new Serializer(new SerializerOptions(knownTypes: new[] { typeof(Something) }))); + CustomInit(new Serializer(SerializerOptions.Default.WithKnownTypes(new[] { typeof(Something) }))); var expected1 = new Something { StringProp = "First" diff --git a/src/Hyperion.Tests/CyclicTests.cs b/src/Hyperion.Tests/CyclicTests.cs index 59bcd4bc..66821777 100644 --- a/src/Hyperion.Tests/CyclicTests.cs +++ b/src/Hyperion.Tests/CyclicTests.cs @@ -19,7 +19,9 @@ public class CyclicTests public void CanSerializeDeepCyclicReferences() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); var root = new Root(); var bar = new Bar(); bar.Self = bar; @@ -40,7 +42,9 @@ public void CanSerializeDeepCyclicReferences() public void CanSerializeDictionaryPreserveObjectReferences() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); var arr1 = new[] {1, 2, 3}; var arr2 = new[] { 1, 2, 3 }; @@ -64,7 +68,9 @@ public void CanSerializeDictionaryPreserveObjectReferences() public void CanSerializeDictionaryPreserveObjectReferences2() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); var val = new List { "first", "second" }; @@ -91,7 +97,9 @@ public void CanSerializeDictionaryPreserveObjectReferences2() public void CanSerializeCyclicReferences() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default + .WithVersionTolerance(true) + .WithPreserveObjectReferences(true)); var bar = new Bar(); bar.Self = bar; bar.XYZ = 234; @@ -107,7 +115,7 @@ public void CanSerializeCyclicReferences() public void CanSerializeMultiLevelCyclicReferences() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions(preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default.WithPreserveObjectReferences(true)); var a = new A(); var b = new B(); a.B = b; diff --git a/src/Hyperion.Tests/DelegateTests.cs b/src/Hyperion.Tests/DelegateTests.cs index b9cbd332..a60d52f6 100644 --- a/src/Hyperion.Tests/DelegateTests.cs +++ b/src/Hyperion.Tests/DelegateTests.cs @@ -34,7 +34,7 @@ public void Create() public void CanSerializeMemberMethod() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions()); + var serializer = new Serializer(SerializerOptions.Default); Func a = 123.ToString; serializer.Serialize(a, stream); @@ -49,7 +49,7 @@ public void CanSerializeMemberMethod() public void CanSerializeDelegate() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions()); + var serializer = new Serializer(SerializerOptions.Default); Action a = dummy => dummy.Prop = 1; serializer.Serialize(a,stream); @@ -71,7 +71,7 @@ private static int StaticFunc(int a) public void CanSerializeStaticDelegate() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions()); + var serializer = new Serializer(SerializerOptions.Default); Func fun = StaticFunc; @@ -88,7 +88,7 @@ public void CanSerializeStaticDelegate() public void CanSerializeObjectWithClosure() { var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions()); + var serializer = new Serializer(SerializerOptions.Default); var hasClosure = new HasClosure(); hasClosure.Create(); diff --git a/src/Hyperion.Tests/ExpressionTests.cs b/src/Hyperion.Tests/ExpressionTests.cs index e42d5a49..f96e3d81 100644 --- a/src/Hyperion.Tests/ExpressionTests.cs +++ b/src/Hyperion.Tests/ExpressionTests.cs @@ -478,7 +478,7 @@ public void CanSerializeLambdaExpression() [Fact] public void CanSerializeLambdaExpressionContainingGenericMethod() { Expression> expr = dummy => dummy.TestField.Contains('s'); - var serializer = new Serializer(new SerializerOptions(preserveObjectReferences: true)); + var serializer = new Serializer(SerializerOptions.Default.WithPreserveObjectReferences(true)); using (var ms = new MemoryStream()) { serializer.Serialize(expr, ms); diff --git a/src/Hyperion.Tests/IlCompilerTests.cs b/src/Hyperion.Tests/IlCompilerTests.cs index d1cbc3c4..2e8b1d82 100644 --- a/src/Hyperion.Tests/IlCompilerTests.cs +++ b/src/Hyperion.Tests/IlCompilerTests.cs @@ -7,7 +7,6 @@ // ----------------------------------------------------------------------- #endregion -#if NET45 using System; using System.Collections.Generic; using System.IO; @@ -67,7 +66,7 @@ public void CanCallStaticMethodUsingParameter() var a = c.Compile(); var dummy = new Dummy(); a(dummy); - Assert.Equal(true, dummy.BoolField); + Assert.True(dummy.BoolField); } [Fact] @@ -79,7 +78,7 @@ public void CanCallInstanceMethodOnParameter() var a = c.Compile(); var dummy = new Dummy(); a(dummy); - Assert.Equal(true, dummy.BoolField); + Assert.True(dummy.BoolField); } @@ -93,7 +92,7 @@ public void CanModifyParameter() var a = c.Compile(); var dummy = new Dummy(); a(dummy); - Assert.Equal(true,dummy.BoolField); + Assert.True(dummy.BoolField); } [Fact] @@ -112,7 +111,7 @@ public void CanCreateEmptyMethodWithReturnType() c.Emit(b); var a = c.Compile(); var res = a(); - Assert.Equal(true,res); + Assert.True(res); } [Fact] @@ -190,7 +189,7 @@ public void ReadSimulationFakeTupleString() { var value = new FakeTupleString("Hello"); var type = value.GetType(); - var serializer = new Serializer(new SerializerOptions(knownTypes: new List() { type })); + var serializer = new Serializer(SerializerOptions.Default.WithKnownTypes(new List() { type })); var session = new DeserializerSession(serializer); var stream = new MemoryStream(); @@ -211,7 +210,7 @@ public void ReadSimulationOptionString() { var value = FSharpOption.Some("abc"); var type = value.GetType(); - var serializer = new Serializer(new SerializerOptions(knownTypes: new List() { type })); + var serializer = new Serializer(SerializerOptions.Default.WithKnownTypes(new List() { type })); var session = new DeserializerSession(serializer); var stream = new MemoryStream(); @@ -230,7 +229,7 @@ public void ReadSimulationTupleString() { var value = Tuple.Create("Hello"); var type = value.GetType(); - var serializer = new Serializer(new SerializerOptions(knownTypes: new List() { type })); + var serializer = new Serializer(SerializerOptions.Default.WithKnownTypes(new List() { type })); var session = new DeserializerSession(serializer); var stream = new MemoryStream(); @@ -248,7 +247,7 @@ public void ReadSimulationTupleString() [Fact] public void ReadSimulation() { - var serializer = new Serializer(new SerializerOptions(knownTypes:new List() {typeof(Poco)})); + var serializer = new Serializer(SerializerOptions.Default.WithKnownTypes(new List() {typeof(Poco)})); var session = new DeserializerSession(serializer); var stream = new MemoryStream(); var poco = new Poco() @@ -306,4 +305,3 @@ private static ObjectReader GetDelegate(Type type, FieldInfo[] fields, Serialize } } } -#endif \ No newline at end of file diff --git a/src/Hyperion.Tests/InterfaceTests.cs b/src/Hyperion.Tests/InterfaceTests.cs index 0dbaa838..aa086a49 100644 --- a/src/Hyperion.Tests/InterfaceTests.cs +++ b/src/Hyperion.Tests/InterfaceTests.cs @@ -43,7 +43,7 @@ public void CanSerializeInterfaceField() } }; var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions()); + var serializer = new Serializer(SerializerOptions.Default); serializer.Serialize(b, stream); stream.Position = 0; var res = serializer.Deserialize(stream); diff --git a/src/Hyperion.Tests/SurrogateTests.cs b/src/Hyperion.Tests/SurrogateTests.cs index 1fe5e6e5..8286fd5b 100644 --- a/src/Hyperion.Tests/SurrogateTests.cs +++ b/src/Hyperion.Tests/SurrogateTests.cs @@ -105,7 +105,7 @@ public void CanSerializeWithSurrogate() }) }; var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions(surrogates: surrogates)); + var serializer = new Serializer(SerializerOptions.Default.WithSurrogates(surrogates)); var foo = new Foo { Bar = "I will be replaced!" @@ -131,7 +131,7 @@ public void CanSerializeWithInterfaceSurrogate() }) }; var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions(surrogates: surrogates)); + var serializer = new Serializer(SerializerOptions.Default.WithSurrogates(surrogates)); var foo = new Foo { Bar = "I will be replaced!" @@ -157,7 +157,7 @@ public void CanSerializeWithSurrogateInCollection() }) }; var stream = new MemoryStream(); - var serializer = new Serializer(new SerializerOptions(surrogates: surrogates)); + var serializer = new Serializer(SerializerOptions.Default.WithSurrogates(surrogates)); var key = new SurrogatedKey("test key"); var foo = new Foo { diff --git a/src/Hyperion.Tests/UnsupportedTypeSerializerTests.cs b/src/Hyperion.Tests/UnsupportedTypeSerializerTests.cs index b9a1317e..0b75fc3d 100644 --- a/src/Hyperion.Tests/UnsupportedTypeSerializerTests.cs +++ b/src/Hyperion.Tests/UnsupportedTypeSerializerTests.cs @@ -63,10 +63,7 @@ public void DoUnsupportedTypesThrowErrors() /* Copied from MongoDB.Bson.BsonElement - causes failures in the serializer - not sure why */ -#if NET45 [Serializable] -#endif - internal struct TestElement : IComparable, IEquatable { public TestElement(string name, string value) diff --git a/src/Hyperion/Compilation/IlBuilder.cs b/src/Hyperion/Compilation/IlBuilder.cs index 59e65239..995c9581 100644 --- a/src/Hyperion/Compilation/IlBuilder.cs +++ b/src/Hyperion/Compilation/IlBuilder.cs @@ -15,7 +15,6 @@ namespace Hyperion.Compilation { -#if NET45 internal class IlBuilder { private readonly List _expressions = new List(); @@ -181,5 +180,4 @@ public int Convert(int value, Type type) return _expressions.Count - 1; } } -#endif } \ No newline at end of file diff --git a/src/Hyperion/Compilation/IlCompiler.cs b/src/Hyperion/Compilation/IlCompiler.cs index 32aee18c..08d4322c 100644 --- a/src/Hyperion/Compilation/IlCompiler.cs +++ b/src/Hyperion/Compilation/IlCompiler.cs @@ -14,7 +14,6 @@ namespace Hyperion.Compilation { -#if NET45 internal sealed class IlCompiler : IlBuilder, ICompiler { public TDel Compile() @@ -93,5 +92,4 @@ private object BuildSelf() return self; } } -#endif } \ No newline at end of file diff --git a/src/Hyperion/Compilation/IlCompilerContext.cs b/src/Hyperion/Compilation/IlCompilerContext.cs index 5ea7c7c8..60b1b7ec 100644 --- a/src/Hyperion/Compilation/IlCompilerContext.cs +++ b/src/Hyperion/Compilation/IlCompilerContext.cs @@ -14,7 +14,6 @@ namespace Hyperion.Compilation { -#if NET45 internal sealed class IlCompilerContext { private int _stackDepth; @@ -94,5 +93,4 @@ public void EmitCall(OpCode opcode, MethodInfo method, Type[] optionalTypes) _il.EmitCall(opcode, method, optionalTypes); } } -#endif } \ No newline at end of file diff --git a/src/Hyperion/Compilation/IlExpression.cs b/src/Hyperion/Compilation/IlExpression.cs index b864286d..f7fccad5 100644 --- a/src/Hyperion/Compilation/IlExpression.cs +++ b/src/Hyperion/Compilation/IlExpression.cs @@ -14,7 +14,6 @@ namespace Hyperion.Compilation { -#if NET45 internal abstract class IlExpression { public abstract void Emit(IlCompilerContext ctx); @@ -323,5 +322,4 @@ public override void Emit(IlCompilerContext ctx) public override Type Type() => _method.ReturnType; } -#endif } \ No newline at end of file diff --git a/src/Hyperion/Extensions/TypeEx.cs b/src/Hyperion/Extensions/TypeEx.cs index 35c9a7ae..7317d7d9 100644 --- a/src/Hyperion/Extensions/TypeEx.cs +++ b/src/Hyperion/Extensions/TypeEx.cs @@ -97,31 +97,7 @@ public static bool IsHyperionPrimitive(this Type type) //add TypeSerializer with null support } -#if NETSTANDARD16 - //HACK: IsValueType does not exist for netstandard1.6 - private static bool IsValueType(this Type type) - => type.IsSubclassOf(typeof(ValueType)); - - private static bool IsSubclassOf(this Type p, Type c) - => c.IsAssignableFrom(p); - - //HACK: the GetUnitializedObject actually exists in .NET Core, its just not public - private static readonly Func GetUninitializedObjectDelegate = (Func) - typeof(string) - .GetTypeInfo() - .Assembly - .GetType("System.Runtime.Serialization.FormatterServices") - ?.GetTypeInfo() - ?.GetMethod("GetUninitializedObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static) - ?.CreateDelegate(typeof(Func)); - - public static object GetEmptyObject(this Type type) - { - return GetUninitializedObjectDelegate(type); - } -#else public static object GetEmptyObject(this Type type) => System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type); -#endif public static bool IsOneDimensionalArray(this Type type) { @@ -172,25 +148,15 @@ private static Type GetTypeFromManifestName(Stream stream, DeserializerSession s [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool UnsafeInheritanceCheck(Type type) { -#if NETSTANDARD1_6 - if (type.IsValueType()) - return false; - var currentBase = type.DeclaringType; -#else if (type.IsValueType) return false; var currentBase = type.BaseType; -#endif while (currentBase != null) { if (IsDisallowedType(currentBase)) return true; -#if NETSTANDARD1_6 - currentBase = currentBase.DeclaringType; -#else currentBase = currentBase.BaseType; -#endif } return false; diff --git a/src/Hyperion/Hyperion.csproj b/src/Hyperion/Hyperion.csproj index 214e2837..cf7e6700 100644 --- a/src/Hyperion/Hyperion.csproj +++ b/src/Hyperion/Hyperion.csproj @@ -4,24 +4,19 @@ Hyperion Hyperion, fast binary POCO serializer - netstandard1.6;netstandard2.0;net45 + netstandard2.0;net471 true serialization;poco - - - - - - - + + - + @@ -37,16 +32,8 @@ - - $(DefineConstants);NETSTANDARD16 - - - - $(DefineConstants);NETSTANDARD20 - - - - $(DefineConstants);SERIALIZATION;UNSAFE;NET45 + + $(DefineConstants);SERIALIZATION;UNSAFE;NETFX diff --git a/src/Hyperion/Serializer.cs b/src/Hyperion/Serializer.cs index 966b5b53..d77663a2 100644 --- a/src/Hyperion/Serializer.cs +++ b/src/Hyperion/Serializer.cs @@ -40,7 +40,7 @@ public class Serializer internal readonly ConcurrentDictionary AcceptedTypes = new ConcurrentDictionary(); - public Serializer() : this(new SerializerOptions()) + public Serializer() : this(SerializerOptions.Default) { } diff --git a/src/Hyperion/SerializerFactories/AggregateExceptionSerializerFactory.cs b/src/Hyperion/SerializerFactories/AggregateExceptionSerializerFactory.cs index 2ae7a20d..106e5e4c 100644 --- a/src/Hyperion/SerializerFactories/AggregateExceptionSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/AggregateExceptionSerializerFactory.cs @@ -39,29 +39,15 @@ public AggregateExceptionSerializerFactory() } public override bool CanSerialize(Serializer serializer, Type type) => -#if NETSTANDARD16 - false; -#else AggregateExceptionTypeInfo.IsAssignableFrom(type.GetTypeInfo()); -#endif public override bool CanDeserialize(Serializer serializer, Type type) => CanSerialize(serializer, type); -#if NETSTANDARD16 - // Workaround for CoreCLR where FormatterServices.GetUninitializedObject is not public - private static readonly Func GetUninitializedObject = - (Func) - typeof(string).GetTypeInfo().Assembly.GetType("System.Runtime.Serialization.FormatterServices") - .GetMethod("GetUninitializedObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static) - .CreateDelegate(typeof(Func)); -#else private static readonly Func GetUninitializedObject = System.Runtime.Serialization.FormatterServices.GetUninitializedObject; -#endif public override ValueSerializer BuildSerializer(Serializer serializer, Type type, ConcurrentDictionary typeMapping) { -#if !NETSTANDARD1_6 var exceptionSerializer = new ObjectSerializer(type); exceptionSerializer.Initialize((stream, session) => { @@ -121,9 +107,6 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type else typeMapping.TryAdd(type, exceptionSerializer); return exceptionSerializer; -#else - return null; -#endif } } } \ No newline at end of file diff --git a/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs b/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs index aca00d58..9522b230 100644 --- a/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs @@ -37,22 +37,13 @@ public ExceptionSerializerFactory() public override bool CanDeserialize(Serializer serializer, Type type) => CanSerialize(serializer, type); -#if NETSTANDARD16 - // Workaround for CoreCLR where FormatterServices.GetUninitializedObject is not public - private static readonly Func GetUninitializedObject = - (Func) - typeof(string).GetTypeInfo().Assembly.GetType("System.Runtime.Serialization.FormatterServices") - .GetMethod("GetUninitializedObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static) - .CreateDelegate(typeof(Func)); -#else private static readonly Func GetUninitializedObject = System.Runtime.Serialization.FormatterServices.GetUninitializedObject; -#endif public override ValueSerializer BuildSerializer(Serializer serializer, Type type, ConcurrentDictionary typeMapping) { var exceptionSerializer = new ObjectSerializer(type); - var hasDefaultConstructor = type.GetTypeInfo().GetConstructor(new Type[0]) != null; + var hasDefaultConstructor = type.GetTypeInfo().GetConstructor(Type.EmptyTypes) != null; var createInstance = hasDefaultConstructor ? Activator.CreateInstance : GetUninitializedObject; exceptionSerializer.Initialize((stream, session) => @@ -64,11 +55,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type var stackTraceString = stream.ReadString(session); var innerException = stream.ReadObject(session); -#if NETSTANDARD20 _className?.SetValue(exception, className); -#else - _className.SetValue(exception, className); -#endif _message.SetValue(exception, message); _remoteStackTraceString.SetValue(exception, remoteStackTraceString); _stackTraceString.SetValue(exception, stackTraceString); @@ -76,11 +63,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type return exception; }, (stream, exception, session) => { -#if NETSTANDARD20 var className = (string)_className?.GetValue(exception); -#else - var className = (string)_className.GetValue(exception); -#endif var message = (string)_message.GetValue(exception); var remoteStackTraceString = (string)_remoteStackTraceString.GetValue(exception); var stackTraceString = (string)_stackTraceString.GetValue(exception); diff --git a/src/Hyperion/SerializerOptions.cs b/src/Hyperion/SerializerOptions.cs index 0c8694dd..4e3722b2 100644 --- a/src/Hyperion/SerializerOptions.cs +++ b/src/Hyperion/SerializerOptions.cs @@ -16,17 +16,26 @@ namespace Hyperion { public class SerializerOptions { - public static readonly SerializerOptions Default = new SerializerOptions(); + public static readonly SerializerOptions Default = new SerializerOptions( + versionTolerance: false, + preserveObjectReferences: false, + surrogates: null, + serializerFactories: null, + knownTypes: null, + ignoreISerializable: false, + packageNameOverrides: null, + disallowUnsafeTypes: true, + typeFilter: null); internal static List> DefaultPackageNameOverrides() { return new List> { -#if NET45 +#if NETFX str => str.Contains("System.Private.CoreLib,%core%") ? str.Replace("System.Private.CoreLib,%core%", "mscorlib,%core%") : str -#elif NETSTANDARD +#else str => str.Contains("mscorlib,%core%") ? str.Replace("mscorlib,%core%", "System.Private.CoreLib,%core%") : str @@ -34,35 +43,43 @@ internal static List> DefaultPackageNameOverrides() }; } - internal static readonly Surrogate[] EmptySurrogates = new Surrogate[0]; + internal static Surrogate[] EmptySurrogates() => Array.Empty(); - private static readonly ValueSerializerFactory[] DefaultValueSerializerFactories = + private static ValueSerializerFactory[] _defaultValueSerializerFactories; + private static ValueSerializerFactory[] DefaultValueSerializerFactories { - new ConsistentArraySerializerFactory(), - new MethodInfoSerializerFactory(), - new PropertyInfoSerializerFactory(), - new ConstructorInfoSerializerFactory(), - new FieldInfoSerializerFactory(), - new DelegateSerializerFactory(), - new ToSurrogateSerializerFactory(), - new FromSurrogateSerializerFactory(), - new FSharpMapSerializerFactory(), - new FSharpListSerializerFactory(), - //order is important, try dictionaries before enumerables as dicts are also enumerable - new AggregateExceptionSerializerFactory(), - new ExceptionSerializerFactory(), - new ImmutableCollectionsSerializerFactory(), - new ExpandoObjectSerializerFactory(), - new DefaultDictionarySerializerFactory(), - new DictionarySerializerFactory(), - new ArraySerializerFactory(), - new MultipleDimensionalArraySerialzierFactory(), + get + { + if(_defaultValueSerializerFactories == null) + _defaultValueSerializerFactories = new ValueSerializerFactory[] + { + new ConsistentArraySerializerFactory(), + new MethodInfoSerializerFactory(), + new PropertyInfoSerializerFactory(), + new ConstructorInfoSerializerFactory(), + new FieldInfoSerializerFactory(), + new DelegateSerializerFactory(), + new ToSurrogateSerializerFactory(), + new FromSurrogateSerializerFactory(), + new FSharpMapSerializerFactory(), + new FSharpListSerializerFactory(), + //order is important, try dictionaries before enumerables as dicts are also enumerable + new AggregateExceptionSerializerFactory(), + new ExceptionSerializerFactory(), + new ImmutableCollectionsSerializerFactory(), + new ExpandoObjectSerializerFactory(), + new DefaultDictionarySerializerFactory(), + new DictionarySerializerFactory(), + new ArraySerializerFactory(), + new MultipleDimensionalArraySerialzierFactory(), #if SERIALIZATION - new ISerializableSerializerFactory(), //TODO: this will mess up the indexes in the serializer payload + new ISerializableSerializerFactory(), //TODO: this will mess up the indexes in the serializer payload #endif - new EnumerableSerializerFactory(), - - }; + new EnumerableSerializerFactory(), + }; + return _defaultValueSerializerFactories; + } + } internal readonly bool IgnoreISerializable; internal readonly bool PreserveObjectReferences; @@ -123,12 +140,12 @@ public SerializerOptions( ITypeFilter typeFilter) { VersionTolerance = versionTolerance; - Surrogates = surrogates?.ToArray() ?? EmptySurrogates; + Surrogates = surrogates?.ToArray() ?? EmptySurrogates(); //use the default factories + any user defined - ValueSerializerFactories = serializerFactories == null - ? DefaultValueSerializerFactories - : serializerFactories.Concat(DefaultValueSerializerFactories).ToArray(); + ValueSerializerFactories = + serializerFactories?.Concat(DefaultValueSerializerFactories).ToArray() ?? + DefaultValueSerializerFactories; KnownTypes = knownTypes?.ToArray() ?? new Type[] {}; for (var i = 0; i < KnownTypes.Length; i++) diff --git a/src/Hyperion/ValueSerializers/SessionAwareByteArrayRequiringValueSerializer.cs b/src/Hyperion/ValueSerializers/SessionAwareByteArrayRequiringValueSerializer.cs index 57f60559..f5c0ec2f 100644 --- a/src/Hyperion/ValueSerializers/SessionAwareByteArrayRequiringValueSerializer.cs +++ b/src/Hyperion/ValueSerializers/SessionAwareByteArrayRequiringValueSerializer.cs @@ -31,7 +31,7 @@ protected SessionAwareByteArrayRequiringValueSerializer(byte manifest, _write = GetStatic(writeStaticMethod, typeof(void)); _read = GetStatic(readStaticMethod, typeof(TElementType)); -#if NET45 +#if NETFX var c = new IlCompiler>(); #else var c = new Compiler>(); @@ -45,7 +45,7 @@ protected SessionAwareByteArrayRequiringValueSerializer(byte manifest, c.EmitStaticCall(_write, stream, valueTyped, buffer); _writeCompiled = c.Compile(); -#if NET45 +#if NETFX var c2 = new IlCompiler>(); #else var c2 = new Compiler>(); diff --git a/src/Hyperion/ValueSerializers/SessionIgnorantValueSerializer.cs b/src/Hyperion/ValueSerializers/SessionIgnorantValueSerializer.cs index 6ef7865a..6640bd01 100644 --- a/src/Hyperion/ValueSerializers/SessionIgnorantValueSerializer.cs +++ b/src/Hyperion/ValueSerializers/SessionIgnorantValueSerializer.cs @@ -31,7 +31,7 @@ protected SessionIgnorantValueSerializer(byte manifest, _write = GetStatic(writeStaticMethod, typeof(void)); _read = GetStatic(readStaticMethod, typeof(TElementType)); -#if NET45 +#if NETFX var c = new IlCompiler>(); #else var c = new Compiler>(); @@ -44,7 +44,7 @@ protected SessionIgnorantValueSerializer(byte manifest, _writeCompiled = c.Compile(); -#if NET45 +#if NETFX var c2 = new IlCompiler>(); #else var c2 = new Compiler>(); From 5fa59735b9f0528a4325bcd67f3194c7f70d8e04 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Fri, 8 Apr 2022 21:54:43 +0700 Subject: [PATCH 2/2] Bring back net45 target --- src/Hyperion/Hyperion.csproj | 4 ++-- src/Hyperion/SerializerOptions.cs | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Hyperion/Hyperion.csproj b/src/Hyperion/Hyperion.csproj index cf7e6700..36706fce 100644 --- a/src/Hyperion/Hyperion.csproj +++ b/src/Hyperion/Hyperion.csproj @@ -4,7 +4,7 @@ Hyperion Hyperion, fast binary POCO serializer - netstandard2.0;net471 + netstandard2.0;net45 true serialization;poco @@ -32,7 +32,7 @@ - + $(DefineConstants);SERIALIZATION;UNSAFE;NETFX diff --git a/src/Hyperion/SerializerOptions.cs b/src/Hyperion/SerializerOptions.cs index 4e3722b2..590aed2c 100644 --- a/src/Hyperion/SerializerOptions.cs +++ b/src/Hyperion/SerializerOptions.cs @@ -43,7 +43,16 @@ internal static List> DefaultPackageNameOverrides() }; } - internal static Surrogate[] EmptySurrogates() => Array.Empty(); + private static Surrogate[] _emptySurrogate; + internal static Surrogate[] EmptySurrogates + { + get + { + if (_emptySurrogate == null) + _emptySurrogate = new Surrogate[0]; + return _emptySurrogate; + } + } private static ValueSerializerFactory[] _defaultValueSerializerFactories; private static ValueSerializerFactory[] DefaultValueSerializerFactories @@ -140,7 +149,7 @@ public SerializerOptions( ITypeFilter typeFilter) { VersionTolerance = versionTolerance; - Surrogates = surrogates?.ToArray() ?? EmptySurrogates(); + Surrogates = surrogates?.ToArray() ?? EmptySurrogates; //use the default factories + any user defined ValueSerializerFactories =