Skip to content

Instantly share code, notes, and snippets.

@klmr
Created May 16, 2012 13:02
Show Gist options
  • Save klmr/2710177 to your computer and use it in GitHub Desktop.
Save klmr/2710177 to your computer and use it in GitHub Desktop.
List nested types
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
Copy link

leppie commented May 16, 2012

Small bug: it is not checking the nested types for other nested types :)

@klmr
Copy link
Author

klmr commented May 16, 2012

@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