Skip to content

Instantly share code, notes, and snippets.

@ianwaldrop
Created December 12, 2015 05:15
Show Gist options
  • Save ianwaldrop/ba2c4d51712d0365696d to your computer and use it in GitHub Desktop.
Save ianwaldrop/ba2c4d51712d0365696d to your computer and use it in GitHub Desktop.
Provides a set of extension methods to assist in reflection things.
using System;
using System.Linq;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Utilties for reflection
/// </summary>
public static class ReflectionUtils
{
/// <summary>
/// Get all the fields of a class
/// </summary>
/// <param name="type">Type object of that class</param>
/// <returns></returns>
public static IEnumerable<FieldInfo> GetAllFields(this Type type)
{
if (type == null)
{
throw new ArgumentNullException("type", "Cannot get fields of a null type");
}
BindingFlags flags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.DeclaredOnly;
return type.GetFields(flags).Union(GetAllFields(type.BaseType));
}
/// <summary>
/// Get all properties of a class
/// </summary>
/// <param name="type">Type object of that class</param>
/// <returns></returns>
public static IEnumerable<PropertyInfo> GetAllProperties(this Type type)
{
if (type == null)
{
throw new ArgumentNullException("type", "Cannot get properties of a null type");
}
BindingFlags flags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.DeclaredOnly;
return type.GetProperties(flags).Union(GetAllProperties(type.BaseType));
}
/// <summary>
/// Get all constructors of a class
/// </summary>
/// <param name="type">Type object of that class</param>
/// <returns></returns>
public static IEnumerable<ConstructorInfo> GetAllConstructors(this Type type)
{
if (type == null)
{
throw new ArgumentNullException("type", "Cannot get constructors of a null type");
}
BindingFlags flags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.DeclaredOnly;
return type.GetConstructors(flags);
}
/// <summary>
/// Get all methods of a class
/// </summary>
/// <param name="type">Type object for that class</param>
/// <param name="recursive">Include methods of base types</param>
/// <returns></returns>
public static IEnumerable<MethodInfo> GetAllMethods(this Type type, bool recursive = false)
{
if (type == null)
{
return Enumerable.Empty<MethodInfo>();
}
BindingFlags flags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.DeclaredOnly;
return recursive ? type.GetMethods(flags).Union(GetAllMethods(type.BaseType, recursive)) : type.GetMethods(flags);
}
public static IEnumerable<Type> GetAllTypes(this Assembly assembly)
{
if (assembly == null)
{
throw new ArgumentNullException("assembly", "Cannot get types of a null assembly");
}
return assembly.GetTypes();
}
public static IEnumerable<Assembly> GetAllAssemblies()
{
return AppDomain.CurrentDomain.GetAssemblies();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment