Skip to content

Commit da9286d

Browse files
BDisptig
andauthored
Fixes #4231. NativeAot project throws when running the published executable (#4232)
* Fixes #4231. NativeAot project throws when running the published executable * Code cleanup --------- Co-authored-by: Tig <[email protected]>
1 parent 51dda7e commit da9286d

File tree

8 files changed

+122
-13
lines changed

8 files changed

+122
-13
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
22

3-
dotnet clean
4-
dotnet build
3+
dotnet clean -c Debug
4+
dotnet build -c Debug
55
dotnet publish -c Debug -r linux-x64 --self-contained
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
22

3-
dotnet clean
4-
dotnet build
3+
dotnet clean -c Release
4+
dotnet build -c Release
55
dotnet publish -c Release -r linux-x64 --self-contained
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
22

3-
dotnet clean
4-
dotnet build
3+
dotnet clean -c Debug
4+
dotnet build -c Debug
55
dotnet publish -c Debug -r osx-x64 --self-contained
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
22

3-
dotnet clean
4-
dotnet build
3+
dotnet clean -c Release
4+
dotnet build -c Release
55
dotnet publish -c Release -r osx-x64 --self-contained

Terminal.Gui/Configuration/DeepCloner.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,37 @@ private static object CloneDictionary (object source, ConcurrentDictionary<objec
278278

279279
// Determine dictionary type and comparer
280280
Type [] genericArgs = type.GetGenericArguments ();
281-
Type dictType = genericArgs.Length == 2
282-
? typeof (Dictionary<,>).MakeGenericType (genericArgs)
283-
: typeof (Dictionary<object, object>);
281+
Type dictType;
282+
283+
if (genericArgs.Length == 2)
284+
{
285+
if (type.GetGenericTypeDefinition () == typeof (Dictionary<,>))
286+
{
287+
dictType = typeof (Dictionary<,>).MakeGenericType (genericArgs);
288+
}
289+
else if (type.GetGenericTypeDefinition () == typeof (ConcurrentDictionary<,>))
290+
{
291+
dictType = typeof (ConcurrentDictionary<,>).MakeGenericType (genericArgs);
292+
}
293+
else
294+
{
295+
throw new InvalidOperationException (
296+
$"Unsupported dictionary type: {type}. Only Dictionary<,> and ConcurrentDictionary<,> are supported.");
297+
}
298+
}
299+
else
300+
{
301+
dictType = typeof (Dictionary<object, object>);
302+
}
303+
284304
object? comparer = type.GetProperty ("Comparer")?.GetValue (source);
285305

286306
// Create a temporary dictionary to hold cloned key-value pairs
287307
IDictionary tempDict = CreateDictionaryInstance (dictType, comparer);
288308
visited.TryAdd (source, tempDict);
289309

290-
291310
object? lastKey = null;
311+
292312
try
293313
{
294314
// Clone all key-value pairs
@@ -311,7 +331,9 @@ private static object CloneDictionary (object source, ConcurrentDictionary<objec
311331
catch (InvalidOperationException ex)
312332
{
313333
// Handle cases where the dictionary is modified during enumeration
314-
throw new InvalidOperationException ($"Error cloning dictionary ({source}) (last key was \"{lastKey}\"). Ensure the source dictionary is not modified during cloning.", ex);
334+
throw new InvalidOperationException (
335+
$"Error cloning dictionary ({source}) (last key was \"{lastKey}\"). Ensure the source dictionary is not modified during cloning.",
336+
ex);
315337
}
316338

317339
// If the original dictionary type has a parameterless constructor, create a new instance

Tests/UnitTestsParallelizable/Configuration/DeepClonerTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,4 +551,91 @@ public void LargeObject_PerformsWithinLimit ()
551551
Assert.True (stopwatch.ElapsedMilliseconds < 1000); // Ensure it completes within 1 second
552552
}
553553

554+
[Fact]
555+
public void CloneDictionary_ShouldClone_NormalDictionary ()
556+
{
557+
// Arrange: A supported generic dictionary
558+
var original = new Dictionary<string, int>
559+
{
560+
["one"] = 1,
561+
["two"] = 2,
562+
["three"] = 3
563+
};
564+
565+
// Act
566+
var cloned = DeepCloner.DeepClone (original);
567+
568+
// Assert
569+
Assert.NotNull (cloned);
570+
Assert.NotSame (original, cloned); // must be a new instance
571+
Assert.Equal (original, cloned); // must have same contents
572+
Assert.Equal (original.Count, cloned.Count);
573+
Assert.Equal (original ["one"], cloned ["one"]);
574+
Assert.Equal (original ["two"], cloned ["two"]);
575+
Assert.Equal (original ["three"], cloned ["three"]);
576+
}
577+
578+
[Fact]
579+
public void CloneDictionary_ShouldClone_ConcurrentDictionary ()
580+
{
581+
// Arrange
582+
var original = new ConcurrentDictionary<string, string>
583+
{
584+
["a"] = "alpha",
585+
["b"] = "beta"
586+
};
587+
588+
// Act
589+
var cloned = DeepCloner.DeepClone (original);
590+
591+
// Assert
592+
Assert.NotSame (original, cloned);
593+
Assert.Equal (original, cloned);
594+
}
595+
596+
[Fact]
597+
public void CloneDictionary_Empty_Dictionary_ShouldWork ()
598+
{
599+
// Arrange
600+
var original = new Dictionary<int, string> ();
601+
602+
// Act
603+
var cloned = DeepCloner.DeepClone (original);
604+
605+
// Assert
606+
Assert.NotSame (original, cloned);
607+
Assert.Empty (cloned!);
608+
}
609+
610+
[Fact]
611+
public void CloneDictionary_Empty_ConcurrentDictionary_ShouldWork ()
612+
{
613+
// Arrange
614+
var original = new ConcurrentDictionary<int, string> ();
615+
616+
// Act
617+
var cloned = DeepCloner.DeepClone (original);
618+
619+
// Assert
620+
Assert.NotSame (original, cloned);
621+
Assert.Empty (cloned!);
622+
}
623+
624+
[Fact]
625+
public void CloneDictionary_With_Unsupported_Dictionary_Throws ()
626+
{
627+
// Arrange: A generic dictionary type (SortedDictionary for example)
628+
var unsupportedDict = new SortedDictionary<int, string>
629+
{
630+
{ 1, "A" },
631+
{ 2, "B" }
632+
};
633+
634+
// Act & Assert: DeepCloner should throw
635+
Assert.ThrowsAny<InvalidOperationException> (() =>
636+
{
637+
// This should throw, because DeepCloner does not support SortedDictionary
638+
_ = DeepCloner.DeepClone (unsupportedDict);
639+
});
640+
}
554641
}
17.4 KB
Binary file not shown.
1.78 KB
Binary file not shown.

0 commit comments

Comments
 (0)