How to query an assembly's metadata with Reflection (LINQ)

You use the .NET reflection APIs to examine the metadata in a .NET assembly and create collections of types, type members, and parameters that are in that assembly. Because these collections support the generic IEnumerable<T> interface, they can be queried by using LINQ.

The following example shows how LINQ can be used with reflection to retrieve specific metadata about methods that match a specified search criterion. In this case, the query finds the names of all the methods in the assembly that return enumerable types such as arrays.

Assembly assembly = Assembly.Load("System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e");
var pubTypesQuery = from type in assembly.GetTypes()
                    where type.IsPublic
                    from method in type.GetMethods()
                    where method.ReturnType.IsArray == true
                        || (method.ReturnType.GetInterface(
                            typeof(System.Collections.Generic.IEnumerable<>).FullName!) != null
                        && method.ReturnType.FullName != "System.String")
                    group method.ToString() by type.ToString();

foreach (var groupOfMethods in pubTypesQuery)
{
    Console.WriteLine("Type: {0}", groupOfMethods.Key);
    foreach (var method in groupOfMethods)
    {
        Console.WriteLine("  {0}", method);
    }
}

The example uses the Assembly.GetTypes method to return an array of types in the specified assembly. The where filter is applied so that only public types are returned. For each public type, a subquery is generated by using the MethodInfo array that is returned from the Type.GetMethods call. These results are filtered to return only those methods whose return type is an array or else a type that implements IEnumerable<T>. Finally, these results are grouped by using the type name as a key.