fix #216
This commit is contained in:
parent
48ae58196d
commit
ef1b670b8b
|
|
@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Blazor.Components
|
||||||
{
|
{
|
||||||
// Do all the reflection up front
|
// Do all the reflection up front
|
||||||
var injectableProperties = type.GetTypeInfo()
|
var injectableProperties = type.GetTypeInfo()
|
||||||
.GetProperties(_injectablePropertyBindingFlags)
|
.GetPropertiesIncludingInherited(_injectablePropertyBindingFlags)
|
||||||
.Where(p => p.GetCustomAttribute<InjectAttribute>() != null);
|
.Where(p => p.GetCustomAttribute<InjectAttribute>() != null);
|
||||||
var injectables = injectableProperties.Select(property =>
|
var injectables = injectableProperties.Select(property =>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Blazor
|
||||||
|
{
|
||||||
|
public static class TypeInfoExtensions
|
||||||
|
{
|
||||||
|
public static IEnumerable<PropertyInfo> GetPropertiesIncludingInherited(this TypeInfo typeInfo, BindingFlags bindingFlags)
|
||||||
|
{
|
||||||
|
while (typeInfo != null)
|
||||||
|
{
|
||||||
|
var properties = typeInfo.GetProperties(bindingFlags)
|
||||||
|
.Where(prop => prop.ReflectedType == prop.DeclaringType);
|
||||||
|
foreach (var property in properties)
|
||||||
|
yield return property;
|
||||||
|
|
||||||
|
typeInfo = typeInfo.BaseType?.GetTypeInfo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -117,6 +117,20 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
||||||
Assert.Same(serviceInstance, instance.MyService);
|
Assert.Same(serviceInstance, instance.MyService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SetsPrivateInheritedInjectableProperties()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var serviceInstance = new MyServiceImplementation();
|
||||||
|
_serviceProvider.AddService<IMyService>(serviceInstance);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var instance = InstantiateComponent<HasInheritedPrivateInjectableProperty>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Same(serviceInstance, instance.PrivateMyService);
|
||||||
|
}
|
||||||
|
|
||||||
private T InstantiateComponent<T>() where T: IComponent
|
private T InstantiateComponent<T>() where T: IComponent
|
||||||
=> _renderer.InstantiateComponent<T>();
|
=> _renderer.InstantiateComponent<T>();
|
||||||
|
|
||||||
|
|
@ -143,6 +157,15 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
||||||
[Inject] public IMyService MyService { get; set; }
|
[Inject] public IMyService MyService { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class HasPrivateInjectableProperty : TestComponent
|
||||||
|
{
|
||||||
|
[Inject] private IMyService MyService { get; set; }
|
||||||
|
|
||||||
|
public IMyService PrivateMyService => MyService;
|
||||||
|
}
|
||||||
|
|
||||||
|
class HasInheritedPrivateInjectableProperty : HasPrivateInjectableProperty { }
|
||||||
|
|
||||||
class HasManyInjectableProperties : TestComponent
|
class HasManyInjectableProperties : TestComponent
|
||||||
{
|
{
|
||||||
[Inject] public IMyService PublicReadWrite { get; set; }
|
[Inject] public IMyService PublicReadWrite { get; set; }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue