Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force EnableUnsafeBinaryFormatterSerialization using SetSwitch - BinaryFormatter Issue #1349

Merged
merged 5 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion source/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<DebugType>embedded</DebugType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion source/Nuke.Common/Nuke.Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<MSBuildWarningsAsErrors>$(MSBuildWarningsAsErrors);CS8785</MSBuildWarningsAsErrors>
<NoWarn>$(NoWarn);SYSLIB0050;SYSLIB0051</NoWarn>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>

</Project>
31 changes: 31 additions & 0 deletions source/Nuke.Tooling.Tests/NewInstanceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2023 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using FluentAssertions;
using Nuke.Common.Tooling;
using System;
using Xunit;

namespace Nuke.Common.Tests;

public class NewInstanceTest
{
[Serializable]
public class SimpleEntity : ISettingsEntity
{
public int Integer { get; set; }
public string String { get; set; }
}

[Fact]
public void TestSimpleEntity()
{
var entity = new SimpleEntity { Integer = 1, String = "test" };
var newInstance = entity.NewInstance();

newInstance.Integer.Should().Be(entity.Integer);
newInstance.String.Should().Be(entity.String);
}

}
4 changes: 2 additions & 2 deletions source/Nuke.Tooling.Tests/Nuke.Tooling.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 8 additions & 5 deletions source/Nuke.Tooling/SettingsEntity.NewInstance.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Maintainers of NUKE.
// Copyright 2024 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

Expand All @@ -7,6 +7,7 @@
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using JetBrains.Annotations;

#pragma warning disable SYSLIB0011

namespace Nuke.Common.Tooling;
Expand All @@ -17,18 +18,20 @@ public static partial class SettingsEntityExtensions
public static T NewInstance<T>(this T settingsEntity)
where T : ISettingsEntity
{
AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", isEnabled: true);

var binaryFormatter = new BinaryFormatter();

using var memoryStream = new MemoryStream();
binaryFormatter.Serialize(memoryStream, settingsEntity);
memoryStream.Seek(offset: 0, loc: SeekOrigin.Begin);

var newInstance = (T) binaryFormatter.Deserialize(memoryStream);
var newInstance = (T)binaryFormatter.Deserialize(memoryStream);
if (newInstance is ToolSettings toolSettings)
{
toolSettings.ProcessArgumentConfigurator = ((ToolSettings) (object) settingsEntity).ProcessArgumentConfigurator;
toolSettings.ProcessLogger = ((ToolSettings) (object) settingsEntity).ProcessLogger;
toolSettings.ProcessExitHandler = ((ToolSettings) (object) settingsEntity).ProcessExitHandler;
toolSettings.ProcessArgumentConfigurator = ((ToolSettings)(object)settingsEntity).ProcessArgumentConfigurator;
toolSettings.ProcessLogger = ((ToolSettings)(object)settingsEntity).ProcessLogger;
toolSettings.ProcessExitHandler = ((ToolSettings)(object)settingsEntity).ProcessExitHandler;
}

return newInstance;
Expand Down
Loading