Skip to content
Open
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
11 changes: 10 additions & 1 deletion src/NSubstitute/Core/DefaultForType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ private object DefaultInstanceOfValueType(Type returnType)
return BoxedDouble;
}

return Activator.CreateInstance(returnType)!;
Copy link
Contributor

@304NotModified 304NotModified Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm doubting if we should use Activator.CreateInstance(returnType)!; for non-stucts, to keep things 100% the same for the previous cases.

Although all tests works, so not sure.

@nsubstitute/core-team any opinion on this?

// Need to have a special case for Nullable<T> to return null.
if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return null!;
}
#if NET5_0_OR_GREATER
return System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(returnType);
#else
return System.Runtime.Serialization.FormatterServices.GetUninitializedObject(returnType);
#endif
}
}
3 changes: 2 additions & 1 deletion src/NSubstitute/Routing/AutoValues/AutoObservableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public bool CanProvideValueFor(Type type) =>

private static object? GetDefault(Type type)
{
return type.GetTypeInfo().IsValueType ? Activator.CreateInstance(type) : null;
var defaultForType = new DefaultForType();
return defaultForType.GetDefaultFor(type);
}
}
4 changes: 3 additions & 1 deletion src/NSubstitute/Routing/AutoValues/AutoTaskProvider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using NSubstitute.Core;
using System.Reflection;

namespace NSubstitute.Routing.AutoValues;
Expand Down Expand Up @@ -32,6 +33,7 @@ public object GetValue(Type type)

private static object? GetDefault(Type type)
{
return type.GetTypeInfo().IsValueType ? Activator.CreateInstance(type) : null;
var defaultForType = new DefaultForType();
return defaultForType.GetDefaultFor(type);
}
}
21 changes: 21 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,27 @@ public void Does_support_out_method_with_base_override()
Assert.That(outArg, Is.EqualTo(4));
}

[Test]
public void Any_on_struct_with_default_constructor_should_work()
{
var something = Substitute.For<IWithStructWithDefaultConstructor>();
Assert.DoesNotThrow(() => something.MethodWithStruct(Arg.Any<StructWithDefaultConstructor>()));
}

public struct StructWithDefaultConstructor
{
public int Value { get; set; }
public StructWithDefaultConstructor()
{
Value = 42;
}
}

public interface IWithStructWithDefaultConstructor
{
void MethodWithStruct(StructWithDefaultConstructor arg);
}

[SetUp]
public void SetUp()
{
Expand Down