Also check non-public `ApiContainer` properties when injecting APIs

This commit is contained in:
YoshiRulz 2022-05-18 15:04:20 +10:00
parent aa135fc0dd
commit 71150c60d3
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 6 additions and 3 deletions

View File

@ -35,7 +35,7 @@ namespace BizHawk.Client.Common
Type targetType = target.GetType();
object[] tmp = new object[1];
targetType.GetProperties().FirstOrDefault(pi => pi.PropertyType == typeof(ApiContainer))
targetType.GetProperties(ReflectionExtensions.DI_TARGET_PROPS).FirstOrDefault(pi => pi.PropertyType == typeof(ApiContainer))
?.SetValue(target, source.Container);
foreach (var propinfo in targetType.GetPropertiesWithAttrib(typeof(RequiredApiAttribute)))

View File

@ -11,15 +11,18 @@ namespace BizHawk.Common.ReflectionExtensions
/// </summary>
public static class ReflectionExtensions
{
/// <summary>filter used when looking for <c>[RequiredApi]</c> et al. by reflection for dependency injection</summary>
public const BindingFlags DI_TARGET_PROPS = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
public static IEnumerable<PropertyInfo> GetPropertiesWithAttrib(this Type type, Type attributeType)
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
return type.GetProperties(DI_TARGET_PROPS)
.Where(p => p.GetCustomAttributes(attributeType, false).Length > 0);
}
public static IEnumerable<MethodInfo> GetMethodsWithAttrib(this Type type, Type attributeType)
{
return type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
return type.GetMethods(DI_TARGET_PROPS)
.Where(p => p.GetCustomAttributes(attributeType, false).Length > 0);
}