Skip to content

Commit a231d65

Browse files
committed
Little change using tail recursion
1 parent d50ccf0 commit a231d65

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

CommandLine.sln

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FSharp", "FSharp", "{751E63
2020
EndProject
2121
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{C37353EF-032B-484B-B0BD-ABDED8589732}"
2222
ProjectSection(SolutionItems) = preProject
23+
.nuget\.gitignore = .nuget\.gitignore
24+
.nuget\NuGet.Config = .nuget\NuGet.Config
25+
.nuget\NuGet.exe = .nuget\NuGet.exe
26+
.nuget\NuGet.targets = .nuget\NuGet.targets
2327
.nuget\packages.config = .nuget\packages.config
2428
EndProjectSection
2529
EndProject

src/CommandLine/Core/ReflectionExtensions.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal static class ReflectionExtensions
1414
{
1515
public static IEnumerable<T> GetSpecifications<T>(this Type type, Func<PropertyInfo, T> selector)
1616
{
17-
return from pi in GetCandidateTypes(type).SelectMany(x => x.GetProperties())
17+
return from pi in type.FlattenHierarchy().SelectMany(x => x.GetProperties())
1818
let attrs = pi.GetCustomAttributes(true)
1919
where
2020
attrs.OfType<OptionAttribute>().Any() ||
@@ -23,17 +23,26 @@ group pi by pi.Name into g
2323
select selector(g.First());
2424
}
2525

26-
private static IEnumerable<Type> GetCandidateTypes(System.Type type)
26+
private static IEnumerable<Type> FlattenHierarchy(this Type type)
2727
{
28-
while (type != null)
28+
if (type == null)
2929
{
30-
yield return type;
31-
foreach (var @interface in type.GetInterfaces())
32-
{
33-
yield return @interface;
34-
}
35-
type = type.BaseType;
30+
yield break;
3631
}
32+
yield return type;
33+
foreach (var @interface in type.SafeGetInterfaces())
34+
{
35+
yield return @interface;
36+
}
37+
foreach (var @interface in FlattenHierarchy(type.BaseType))
38+
{
39+
yield return @interface;
40+
}
41+
}
42+
43+
private static IEnumerable<Type> SafeGetInterfaces(this Type type)
44+
{
45+
return type == null ? Enumerable.Empty<Type>() : type.GetInterfaces();
3746
}
3847

3948
public static TargetType ToTargetType(this Type type)

0 commit comments

Comments
 (0)