{
public class ClassWithServiceAndOptionalArgsCtorWithStructs
{
+ public DateTime DateTime { get; }
public DateTime DateTimeDefault { get; }
+ public TimeSpan TimeSpan { get; }
+ public TimeSpan TimeSpanDefault { get; }
+ public DateTimeOffset DateTimeOffset { get; }
+ public DateTimeOffset DateTimeOffsetDefault { get; }
+ public Guid Guid { get; }
+ public Guid GuidDefault { get; }
+ public CustomStruct CustomStructValue { get; }
+ public CustomStruct CustomStructDefault { get; }
public ClassWithServiceAndOptionalArgsCtorWithStructs(IFakeService fake,
DateTime dateTime = new DateTime(),
CustomStruct customStructDefault = default(CustomStruct)
)
{
+ DateTime = dateTime;
DateTimeDefault = dateTimeDefault;
+ TimeSpan = timeSpan;
+ TimeSpanDefault = timeSpanDefault;
+ DateTimeOffset = dateTimeOffset;
+ DateTimeOffsetDefault = dateTimeOffsetDefault;
+ Guid = guid;
+ GuidDefault = guidDefault;
+ CustomStructValue = customStruct;
+ CustomStructDefault = customStructDefault;
}
public struct CustomStruct { }
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IFakeService, FakeService>();
+ serviceCollection.AddSingleton<ClassWithServiceAndOptionalArgsCtorWithStructs>();
+
+ var provider = CreateServiceProvider(serviceCollection);
+ var service = provider.GetService<ClassWithServiceAndOptionalArgsCtorWithStructs>();
+ Assert.NotNull(service);
+ }
+
+ [Fact]
+ public void ResolvesServiceMixedServiceAndOptionalStructConstructorArgumentsReliably()
+ {
+ var serviceCollection = new ServiceCollection();
+ serviceCollection.AddSingleton<IFakeService, FakeService>();
serviceCollection.AddTransient<ClassWithServiceAndOptionalArgsCtorWithStructs>();
var provider = CreateServiceProvider(serviceCollection);
var service = provider.GetService<ClassWithServiceAndOptionalArgsCtorWithStructs>();
Assert.NotNull(service);
- Assert.Equal(default, service.DateTimeDefault);
+ Assert.Equal(new DateTime(), service.DateTime);
+ Assert.Equal(default(DateTime), service.DateTimeDefault);
+ Assert.Equal(new TimeSpan(), service.TimeSpan);
+ Assert.Equal(default(TimeSpan), service.TimeSpanDefault);
+ Assert.Equal(new DateTimeOffset(), service.DateTimeOffset);
+ Assert.Equal(default(DateTimeOffset), service.DateTimeOffsetDefault);
+ Assert.Equal(new Guid(), service.Guid);
+ Assert.Equal(default(Guid), service.GuidDefault);
+ Assert.Equal(new ClassWithServiceAndOptionalArgsCtorWithStructs.CustomStruct(), service.CustomStructValue);
+ Assert.Equal(default(ClassWithServiceAndOptionalArgsCtorWithStructs.CustomStruct), service.CustomStructDefault);
}
}