Skip to content

Instantly share code, notes, and snippets.

@kg
Forked from TryJSIL/Reflection.cs
Created April 28, 2012 09:30
Show Gist options
  • Save kg/2517467 to your computer and use it in GitHub Desktop.
Save kg/2517467 to your computer and use it in GitHub Desktop.
Reflection
using System;
using System.Reflection;
using System.Collections.Generic;
public static class Program {
public static void Main (string[] args) {
Common.Util.ListMembers<MethodInfo>(
typeof(T),
BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public
);
Common.Util.ListMembers<MethodInfo>(
typeof(T),
BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.NonPublic
);
Common.Util.ListMembers<MethodInfo>(
typeof(T),
BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public
);
Common.Util.ListMembers<MethodInfo>(
typeof(T),
BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic
);
}
}
public class T {
static T () {
}
public static void MethodA () {
}
private static void MethodB () {
}
public void MethodC () {
}
private void MethodD () {
}
}
namespace Common {
public static class Util {
public static string[] GetMemberNames<T> (Type type, BindingFlags flags) where T : MemberInfo {
var members = type.GetMembers(flags);
int count = 0;
for (int i = 0; i < members.Length; i++) {
if (members[i] is T)
count += 1;
}
var names = new string[count];
for (int i = 0, j = 0; i < members.Length; i++) {
T t = (members[i] as T);
if ((object)t == null)
continue;
names[j] = t.Name;
j += 1;
}
Array.Sort(names);
return names;
}
public static void ListMembers<T> (Type type, BindingFlags flags) where T : MemberInfo {
var methodNames = GetMemberNames<T>(type, flags);
Console.WriteLine();
foreach (var methodName in methodNames)
Console.WriteLine(methodName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment