Skip to content

Instantly share code, notes, and snippets.

Created March 2, 2022 17:42
What would you like to do?
C# function to get extension methods from an assembly.
// Adapted from
static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly, Type extendedType)
return assembly
.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