Created
April 27, 2012 13:08
-
-
Save TryJSIL/2509044 to your computer and use it in GitHub Desktop.
Reflection
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.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