Created
May 16, 2012 13:02
-
-
Save klmr/2710177 to your computer and use it in GitHub Desktop.
List nested types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace CsConsole | |
{ | |
class MainClass | |
{ | |
static void WalkTypes(IEnumerable<Type> types, Action<Type> action) { | |
foreach (var nested in types) | |
action(nested); | |
} | |
static void PrintNestedTypesOfNonGeneric(System.Reflection.Assembly assembly) { | |
Action<Type> visitor; | |
visitor = (type) => { | |
if (type.Namespace != null && type.Namespace.StartsWith("Mono.")) | |
return; | |
if (type.IsGenericType) | |
return; | |
var allNested = from nested in type.GetNestedTypes() | |
where ! nested.IsEnum && nested.IsNestedPublic | |
select nested; | |
foreach (var nested in allNested) | |
Console.WriteLine(nested); | |
WalkTypes(allNested, visitor); | |
}; | |
WalkTypes(assembly.GetTypes(), visitor); | |
} | |
public static void Main (string[] args) | |
{ | |
var mscorlib = System.Reflection.Assembly.Load("mscorlib"); | |
var sys = System.Reflection.Assembly.Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); | |
foreach (var assembly in new [] {mscorlib, sys}) | |
PrintNestedTypesOfNonGeneric(assembly); | |
} | |
} | |
} |
@leppie, See update. Over-engineering for the win. Oh my. I should have used a combinator to declare the recursive lambda … ;-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small bug: it is not checking the nested types for other nested types :)