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<T> slot legitimately holds.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-14 14:31:45 +02:00
commit 04c7c6bcd8
2 changed files with 12 additions and 4 deletions

View file

@ -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<T> 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.

View file

@ -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<T> element, not a failure");
}
[Test]