Skip to content

Instantly share code, notes, and snippets.

@mminer
Created March 2, 2022 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mminer/e2741146d63a604164399c8748afdaa8 to your computer and use it in GitHub Desktop.
Save mminer/e2741146d63a604164399c8748afdaa8 to your computer and use it in GitHub Desktop.
C# function to get extension methods from an assembly.
// Adapted from https://stackoverflow.com/a/299526
static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly, Type extendedType)
{
return assembly
.GetTypes()
.Where(type => type.IsSealed && !type.IsGenericType && !type.IsNested)
.SelectMany(type => type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static))
.Where(method => method.IsDefined(typeof(ExtensionAttribute), false) &&
method.GetParameters()[0].ParameterType == extendedType);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment