未验证 提交 2205465e 编写于 作者: L LateApexEarlySpeed 提交者: GitHub

lateapexearlyspeed-issue-40848 Check open generic type arity mismatch… (#51167)

上级 282c394b
......@@ -134,6 +134,9 @@
<data name="OpenGenericServiceRequiresOpenGenericImplementation" xml:space="preserve">
<value>Open generic service type '{0}' requires registering an open generic implementation type.</value>
</data>
<data name="ArityOfOpenGenericServiceNotEqualArityOfOpenGenericImplementation" xml:space="preserve">
<value>Arity of open generic service type '{0}' does not equal arity of open generic implementation type '{1}'.</value>
</data>
<data name="TypeCannotBeActivated" xml:space="preserve">
<value>Cannot instantiate implementation type '{0}' for service type '{1}'.</value>
</data>
......
......@@ -40,14 +40,20 @@ private void Populate()
if (implementationType == null || !implementationType.IsGenericTypeDefinition)
{
throw new ArgumentException(
SR.Format(SR.OpenGenericServiceRequiresOpenGenericImplementation, descriptor.ServiceType),
SR.Format(SR.OpenGenericServiceRequiresOpenGenericImplementation, serviceType),
"descriptors");
}
if (implementationType.IsAbstract || implementationType.IsInterface)
{
throw new ArgumentException(
SR.Format(SR.TypeCannotBeActivated, descriptor.ImplementationType, descriptor.ServiceType));
SR.Format(SR.TypeCannotBeActivated, implementationType, serviceType));
}
if (serviceType.GetGenericArguments().Length != implementationType.GetGenericArguments().Length)
{
throw new ArgumentException(
SR.Format(SR.ArityOfOpenGenericServiceNotEqualArityOfOpenGenericImplementation, serviceType, implementationType), "descriptors");
}
}
else if (descriptor.ImplementationInstance == null && descriptor.ImplementationFactory == null)
......@@ -60,11 +66,11 @@ private void Populate()
implementationType.IsInterface)
{
throw new ArgumentException(
SR.Format(SR.TypeCannotBeActivated, descriptor.ImplementationType, descriptor.ServiceType));
SR.Format(SR.TypeCannotBeActivated, implementationType, serviceType));
}
}
Type cacheKey = descriptor.ServiceType;
Type cacheKey = serviceType;
_descriptorLookup.TryGetValue(cacheKey, out ServiceDescriptorCacheItem cacheItem);
_descriptorLookup[cacheKey] = cacheItem.Add(descriptor);
}
......
......@@ -104,6 +104,31 @@ public void CreatingServiceProviderWithUnresolvableTypesThrows(Type serviceType,
exception.Message);
}
[Theory]
[MemberData(nameof(FailedOpenGenericTypeTestData))]
public void CreatingServiceProviderWithUnresolvableOpenGenericTypesThrows(Type serviceType, Type implementationType, string errorMessage)
{
// Arrange
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient(serviceType, implementationType);
// Act and Assert
var exception = Assert.Throws<ArgumentException>(() => serviceCollection.BuildServiceProvider());
Assert.StartsWith(errorMessage, exception.Message);
}
public static IEnumerable<object[]> FailedOpenGenericTypeTestData
{
get
{
Type serviceType = typeof(IFakeOpenGenericService<>);
// Service type is GenericTypeDefintion, implementation type is ConstructedGenericType
yield return new object[] {serviceType, typeof(ClassWithNoConstraints<string>), $"Open generic service type '{serviceType}' requires registering an open generic implementation type."};
// Service type is GenericTypeDefintion, implementation type has different generic type definition arity
yield return new object[] {serviceType, typeof(FakeOpenGenericServiceWithTwoTypeArguments<,>), $"Arity of open generic service type '{serviceType}' does not equal arity of open generic implementation type '{typeof(FakeOpenGenericServiceWithTwoTypeArguments<,>)}'."};
}
}
[Fact]
public void DoesNotDisposeSingletonInstances()
{
......@@ -938,6 +963,12 @@ private abstract class AbstractFakeOpenGenericService<T> : IFakeOpenGenericServi
public abstract T Value { get; }
}
private class FakeOpenGenericServiceWithTwoTypeArguments<TVal1, TVal2> : IFakeOpenGenericService<TVal1>
{
public TVal1 Value { get; }
public TVal2 Value2 { get; }
}
private class Disposable : IDisposable
{
public bool Disposed { get; set; }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册