Skip to content

Commit

Permalink
NetStandard2.0 and NetCoreApp3.0 supports added (#119)
Browse files Browse the repository at this point in the history
* * Hyperion FSharp test project converted to netstandard2.0

* * The System.TypeInitializationException bug fixed for netcore3.0 projects by removing static keyword from ExceptionTypeInfo and setting it in the constructor

* * netstandard2.0 support added
* netcore3.0 support added

* * test project updated to run netcoreapp3.0 tests as well

* * tests refactored

* * FormatterServices.GetUninitializedObject reverted back to Func in order to prevent runtime exceptions

* Update .gitignore

* * refactoring

* dotnet sdk install url changed in ps and sh scripts
  • Loading branch information
akselarzuman authored and Danthar committed Oct 4, 2019
1 parent f0bd031 commit 2750c72
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,5 @@ FakesAssemblies/
tools/
build/
.nuget/
.dotnet/
.dotnet/
.idea
6 changes: 3 additions & 3 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ Param(
$FakeVersion = "4.61.2"
$NBenchVersion = "1.2.2"
$DotNetChannel = "LTS";
$DotNetVersion = "2.0.0";
$DotNetInstallerUri = "https://raw.githubusercontent.com/dotnet/cli/v$DotNetVersion/scripts/obtain/dotnet-install.ps1";
$DotNetVersion = "3.0.100";
$DotNetInstallerUri = "https://dot.net/v1/dotnet-install.ps1";
$NugetVersion = "4.1.0";
$NugetUrl = "https://dist.nuget.org/win-x86-commandline/v$NugetVersion/nuget.exe"
$ProtobufVersion = "3.2.0"
$DocfxVersion = "2.21.1"
$DocfxVersion = "2.40.5"

# Make sure tools folder exists
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
Expand Down
4 changes: 2 additions & 2 deletions build.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ NUGET_EXE=$TOOLS_DIR/nuget.exe
NUGET_URL=https://dist.nuget.org/win-x86-commandline/v4.1.0/nuget.exe
FAKE_VERSION=4.61.2
FAKE_EXE=$TOOLS_DIR/FAKE/tools/FAKE.exe
DOTNET_VERSION=2.0.3
DOTNET_INSTALLER_URL=https://raw.githubusercontent.com/dotnet/cli/v$DOTNET_VERSION/scripts/obtain/dotnet-install.sh
DOTNET_VERSION=3.0.100
DOTNET_INSTALLER_URL=https://dot.net/v1/dotnet-install.sh
DOTNET_CHANNEL=LTS;
DOCFX_VERSION=2.40.5
DOCFX_EXE=$TOOLS_DIR/docfx.console/tools/docfx.exe
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Hyperion.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void CanSerializeIndexExpression()
Assert.Equal(expr.NodeType, deserialized.NodeType);
Assert.Equal(expr.Type, deserialized.Type);
Assert.Equal(expr.Indexer, deserialized.Indexer);
Assert.Equal(1, deserialized.Arguments.Count);
Assert.Single(deserialized.Arguments);
Assert.Equal(expr.Arguments[0].ConstantValue(), deserialized.Arguments[0].ConstantValue());
var actual = (int[])deserialized.Object.ConstantValue();
Assert.Equal(value[0], actual[0]);
Expand Down Expand Up @@ -301,7 +301,7 @@ public void CanSerializeMethodCallExpression()
Assert.Equal(expr.NodeType, deserialized.NodeType);
Assert.Equal(expr.Method, deserialized.Method);
Assert.Equal(expr.Object.ConstantValue(), deserialized.Object.ConstantValue());
Assert.Equal(1, deserialized.Arguments.Count);
Assert.Single(deserialized.Arguments);
Assert.Equal(expr.Arguments[0].ConstantValue(), deserialized.Arguments[0].ConstantValue());
}
}
Expand Down Expand Up @@ -523,7 +523,7 @@ public void CanSerializeElementInit()
stream.Position = 0;
var deserialized = serializer.Deserialize<ElementInit>(stream);
Assert.Equal(expr.AddMethod, deserialized.AddMethod);
Assert.Equal(1, deserialized.Arguments.Count);
Assert.Single(deserialized.Arguments);
Assert.Equal(expr.Arguments[0].ConstantValue(), deserialized.Arguments[0].ConstantValue());
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Hyperion.Tests/Hyperion.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />

<PropertyGroup>
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
<RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion>
<TargetFrameworks>net461;netcoreapp2.0;netcoreapp3.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 0 additions & 4 deletions src/Hyperion/Extensions/ReflectionEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
using System.Linq;
using System.Reflection;

#if SERIALIZATION

#endif

namespace Hyperion.Extensions
{
internal static class BindingFlagsEx
Expand Down
11 changes: 2 additions & 9 deletions src/Hyperion/Extensions/TypeEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
using System.Reflection;
using System.Text.RegularExpressions;

#if SERIALIZATION
using System.Runtime.Serialization;
#endif

namespace Hyperion.Extensions
{
internal static class TypeEx
Expand Down Expand Up @@ -70,7 +66,7 @@ public static bool IsHyperionPrimitive(this Type type)
//add TypeSerializer with null support
}

#if !SERIALIZATION
#if NETSTANDARD16
//HACK: the GetUnitializedObject actually exists in .NET Core, its just not public
private static readonly Func<Type, object> getUninitializedObjectDelegate = (Func<Type, object>)
typeof(string)
Expand All @@ -86,10 +82,7 @@ public static object GetEmptyObject(this Type type)
return getUninitializedObjectDelegate(type);
}
#else
public static object GetEmptyObject(this Type type)
{
return FormatterServices.GetUninitializedObject(type);
}
public static object GetEmptyObject(this Type type) => System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type);
#endif

public static bool IsOneDimensionalArray(this Type type)
Expand Down
16 changes: 15 additions & 1 deletion src/Hyperion/Hyperion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<AssemblyTitle>Hyperion</AssemblyTitle>
<Description>Hyperion, fast binary POCO serializer</Description>
<TargetFrameworks>netstandard1.6;net452</TargetFrameworks>
<TargetFrameworks>netstandard1.6;netstandard2.0;net452</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageTags>serialization;poco</PackageTags>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.6' ">1.6.1</NetStandardImplicitPackageVersion>
Expand All @@ -14,12 +14,26 @@
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.3.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.3.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Runtime" Version="4.3.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.3.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>


<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
<DefineConstants>$(DefineConstants);NETSTANDARD16</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);NETSTANDARD20</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net452' ">
<DefineConstants>$(DefineConstants);SERIALIZATION;UNSAFE;NET45</DefineConstants>
</PropertyGroup>
Expand Down
18 changes: 15 additions & 3 deletions src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,23 @@ 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<Type, object> GetUninitializedObject =
(Func<Type, object>)
typeof(string).GetTypeInfo().Assembly.GetType("System.Runtime.Serialization.FormatterServices")
.GetMethod("GetUninitializedObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
.CreateDelegate(typeof(Func<Type, object>));
#else
private static readonly Func<Type,object> GetUninitializedObject = System.Runtime.Serialization.FormatterServices.GetUninitializedObject;
#endif

public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var exceptionSerializer = new ObjectSerializer(type);
var hasDefaultConstructor = type.GetTypeInfo().GetConstructor(new Type[0]) != null;
var createInstance = hasDefaultConstructor ? Activator.CreateInstance : GetUninitializedObject;
var createInstance = hasDefaultConstructor ? Activator.CreateInstance : GetUninitializedObject;

exceptionSerializer.Initialize((stream, session) =>
{
Expand All @@ -60,15 +64,23 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type
var stackTraceString = stream.ReadString(session);
var innerException = stream.ReadObject(session);

_className.SetValue(exception,className);
#if NETSTANDARD20
_className?.SetValue(exception, className);
#else
_className.SetValue(exception, className);
#endif
_message.SetValue(exception, message);
_remoteStackTraceString.SetValue(exception, remoteStackTraceString);
_stackTraceString.SetValue(exception, stackTraceString);
_innerException.SetValue(exception,innerException);
_innerException.SetValue(exception, innerException);
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);
Expand Down

0 comments on commit 2750c72

Please sign in to comment.