This commit is contained in:
Florian Dohrendorf 2018-03-13 01:01:54 +01:00
parent 48ae58196d
commit ef1b670b8b
3 changed files with 46 additions and 1 deletions

View File

@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Blazor.Components
{
// Do all the reflection up front
var injectableProperties = type.GetTypeInfo()
.GetProperties(_injectablePropertyBindingFlags)
.GetPropertiesIncludingInherited(_injectablePropertyBindingFlags)
.Where(p => p.GetCustomAttribute<InjectAttribute>() != null);
var injectables = injectableProperties.Select(property =>
{

View File

@ -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();
}
}
}
}

View File

@ -117,6 +117,20 @@ namespace Microsoft.AspNetCore.Blazor.Test
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
=> _renderer.InstantiateComponent<T>();
@ -143,6 +157,15 @@ namespace Microsoft.AspNetCore.Blazor.Test
[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
{
[Inject] public IMyService PublicReadWrite { get; set; }