From 04c7c6bcd8813ad34e5e73e6fea79f686a146aaa Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 14 Sep 2026 14:31:45 +0200 Subject: [PATCH] Refuse a list Add that creates nothing CreateElement returned null without a notice when the element type itself had no parameterless constructor, and the Studio added that null. The notice now follows the result: nothing made is refused, except the null a Nullable slot legitimately holds. Co-Authored-By: Claude Opus 5 --- src/GameCult.Caching/CultInspectorModel.cs | 12 ++++++++---- .../CultInspectorModelTests.cs | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/GameCult.Caching/CultInspectorModel.cs b/src/GameCult.Caching/CultInspectorModel.cs index bb2fbb3f..22a322d3 100644 --- a/src/GameCult.Caching/CultInspectorModel.cs +++ b/src/GameCult.Caching/CultInspectorModel.cs @@ -366,10 +366,14 @@ namespace GameCult.Caching { if (!ElementChoices(elementType).Contains(choice)) throw new ArgumentException($"{choice.Name} is not a choice for {elementType.Name}.", nameof(choice)); - notice = choice == elementType || choice.GetConstructor(Type.EmptyTypes) != null - ? null - : $"{choice.Name} has no parameterless constructor; nothing was created."; - return notice != null ? null : choice == elementType ? CreateDefault(elementType) : Activator.CreateInstance(choice); + var created = choice == elementType ? CreateDefault(choice) + : choice.GetConstructor(Type.EmptyTypes) == null ? null + : Activator.CreateInstance(choice); + // Null is a value only for a Nullable slot; anywhere else it is nothing made. + notice = created == null && !choice.IsValueType + ? $"{choice.Name} is abstract or has no parameterless constructor; nothing was created." + : null; + return created; } // An integer edit as a value of integerType, clamped to that type's range instead of overflowing. diff --git a/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs b/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs index 6f49ae8e..0e5f42bc 100644 --- a/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs +++ b/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs @@ -81,6 +81,10 @@ namespace GameCult.Caching.Tests Assert.That(model.CreateElement(typeof(InspectPicky), typeof(InspectNeedsArgs), out var refused), Is.Null); Assert.That(refused, Does.Contain("parameterless")); + Assert.That(model.CreateElement(typeof(InspectNeedsArgs), typeof(InspectNeedsArgs), out var bare), Is.Null); + Assert.That(bare, Does.Contain("parameterless"), "a plain element type that cannot be made says so; nothing is added"); + Assert.That(model.CreateElement(typeof(int?), typeof(int?), out var nullable), Is.Null); + Assert.That(nullable, Is.Null, "null is a Nullable element, not a failure"); } [Test]