Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit e652c9a

Browse files
ricaunmatkoch
authored andcommitted
fix(tooling): use AppContext to set EnableUnsafeBinaryFormatterSerialization (#1349)
1 parent cd544d1 commit e652c9a

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

source/Directory.Build.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<DebugType>embedded</DebugType>
99
<PublishRepositoryUrl>true</PublishRepositoryUrl>
1010
<EmbedUntrackedSources>true</EmbedUntrackedSources>
11-
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
1211
</PropertyGroup>
1312

1413
<PropertyGroup>

source/Nuke.Common/Nuke.Common.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
66
<MSBuildWarningsAsErrors>$(MSBuildWarningsAsErrors);CS8785</MSBuildWarningsAsErrors>
77
<NoWarn>$(NoWarn);SYSLIB0050;SYSLIB0051</NoWarn>
8-
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
98
</PropertyGroup>
109

1110
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2023 Maintainers of NUKE.
2+
// Distributed under the MIT License.
3+
// https://github.com/nuke-build/nuke/blob/master/LICENSE
4+
5+
using FluentAssertions;
6+
using Nuke.Common.Tooling;
7+
using System;
8+
using Xunit;
9+
10+
namespace Nuke.Common.Tests;
11+
12+
public class NewInstanceTest
13+
{
14+
[Serializable]
15+
public class SimpleEntity : ISettingsEntity
16+
{
17+
public int Integer { get; set; }
18+
public string String { get; set; }
19+
}
20+
21+
[Fact]
22+
public void TestSimpleEntity()
23+
{
24+
var entity = new SimpleEntity { Integer = 1, String = "test" };
25+
var newInstance = entity.NewInstance();
26+
27+
newInstance.Integer.Should().Be(entity.Integer);
28+
newInstance.String.Should().Be(entity.String);
29+
}
30+
31+
}

source/Nuke.Tooling.Tests/Nuke.Tooling.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

source/Nuke.Tooling/SettingsEntity.NewInstance.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Maintainers of NUKE.
1+
// Copyright 2024 Maintainers of NUKE.
22
// Distributed under the MIT License.
33
// https://github.com/nuke-build/nuke/blob/master/LICENSE
44

@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Runtime.Serialization.Formatters.Binary;
99
using JetBrains.Annotations;
10+
1011
#pragma warning disable SYSLIB0011
1112

1213
namespace Nuke.Common.Tooling;
@@ -17,18 +18,20 @@ public static partial class SettingsEntityExtensions
1718
public static T NewInstance<T>(this T settingsEntity)
1819
where T : ISettingsEntity
1920
{
21+
AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", isEnabled: true);
22+
2023
var binaryFormatter = new BinaryFormatter();
2124

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

26-
var newInstance = (T) binaryFormatter.Deserialize(memoryStream);
29+
var newInstance = (T)binaryFormatter.Deserialize(memoryStream);
2730
if (newInstance is ToolSettings toolSettings)
2831
{
29-
toolSettings.ProcessArgumentConfigurator = ((ToolSettings) (object) settingsEntity).ProcessArgumentConfigurator;
30-
toolSettings.ProcessLogger = ((ToolSettings) (object) settingsEntity).ProcessLogger;
31-
toolSettings.ProcessExitHandler = ((ToolSettings) (object) settingsEntity).ProcessExitHandler;
32+
toolSettings.ProcessArgumentConfigurator = ((ToolSettings)(object)settingsEntity).ProcessArgumentConfigurator;
33+
toolSettings.ProcessLogger = ((ToolSettings)(object)settingsEntity).ProcessLogger;
34+
toolSettings.ProcessExitHandler = ((ToolSettings)(object)settingsEntity).ProcessExitHandler;
3235
}
3336

3437
return newInstance;

0 commit comments

Comments
 (0)