Skip to content
Merged
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?

Copy link
Member

Choose a reason for hiding this comment

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

I'd feel more comfortable if we keep the existing behaviour for non-structs. Never sure how reflection stuff is going to break 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this fixed? This isn't marked as resolved?

Copy link
Contributor

@304NotModified 304NotModified Dec 12, 2025

Choose a reason for hiding this comment

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

So after some reading, this is large change?

 Activator.CreateInstance(returnType)!;

Calls the (default) constructor - that is indeed an issue with structs

System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject

Won't call the constructor.

I'm not sure if this is a good idea.

I'm not sure how DefaultValue is used exactly and if this poses issues.

Anyway, without resolving this discussion, I cannot approve this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The method itself is attempting to get the default value for the type. i.e. 0 for int. For reference types, as you know, this would return null when you try to do object o = default;.

For structs, when you set them to default, for example, when the parameter list has something along the lines of void MyMethod(CancellationToken cancellationToken = default) and then you try to create a matcher against it using Arg.Any<CancellationToken>(), it results in two instances of a struct that will not match, because Arg.Any<CancellationToken>() does not equal default(CancellationToken), which is why GetUninitializedObject is needed.

// 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
Loading