@@ -1109,6 +1109,63 @@ private static IEnumerable<string> GetMembersAcceptingOrReturningType(Type lookF
11091109 private static IEnumerable < Type > GetTypesToTest ( Assembly assembly ) =>
11101110 assembly . GetTypes ( )
11111111 . Where ( t => ! t . HasAttribute < GeneratedCodeAttribute > ( inherit : false )
1112- && ! t . HasAttribute < CompilerGeneratedAttribute > ( inherit : false ) ) ;
1112+ && ! t . HasAttribute < CompilerGeneratedAttribute > ( inherit : false )
1113+ && IsPartOfEffectiveApi ( t ) ) ;
1114+
1115+ /// <summary>
1116+ /// Returns whether the type is part of the effective API surface.
1117+ /// <para />
1118+ /// Types that are part of the effective API surface include <c>public</c> types, as well as
1119+ /// nested types that are either <c>public</c>, <c>protected</c>, or <c>protected internal</c> and where the
1120+ /// declaring type hierarchy is public.
1121+ /// </summary>
1122+ /// <param name="type"></param>
1123+ /// <returns></returns>
1124+ private static bool IsPartOfEffectiveApi ( Type type )
1125+ {
1126+ while ( type != null )
1127+ {
1128+ if ( ! type . IsNested )
1129+ {
1130+ return type . IsPublic ;
1131+ }
1132+
1133+ if ( type . IsNestedPublic )
1134+ {
1135+ type = type . DeclaringType ! ;
1136+ continue ;
1137+ }
1138+
1139+ if ( type . IsNestedFamily || type . IsNestedFamORAssem ) // protected or protected internal
1140+ {
1141+ type = type . DeclaringType ! ;
1142+
1143+ // Every containing type must itself be externally inheritable to see protected members
1144+ while ( type != null )
1145+ {
1146+ if ( ! IsExternallyInheritable ( type ) )
1147+ return false ;
1148+
1149+ type = type . DeclaringType ;
1150+ }
1151+
1152+ return true ;
1153+ }
1154+
1155+ return false ;
1156+ }
1157+
1158+ return false ;
1159+ }
1160+
1161+ private static bool IsExternallyInheritable ( Type type )
1162+ {
1163+ if ( ! type . IsNested )
1164+ return type . IsPublic ;
1165+
1166+ return type . IsNestedPublic ||
1167+ type . IsNestedFamily ||
1168+ type . IsNestedFamORAssem ;
1169+ }
11131170 }
11141171}
0 commit comments