Created
March 2, 2022 17:42
-
-
Save mminer/e2741146d63a604164399c8748afdaa8 to your computer and use it in GitHub Desktop.
C# function to get extension methods from an assembly.
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
// 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