An opiniated library for inspecting .NET assembly Metadata
SharpMeta is a .NET library that facilitates the loading and inspection of .NET assemblies using the System.Reflection.MetadataLoadContext.
This library provides a robust and flexible way to load assemblies from specified file paths and directories,
while also providing extension methods for inspecting type and attribute metadata.
Using SharpAssemblyResolver.Builder.AddReferenceDirectories("path/to/target-framework"), SharpMeta can load and inspect .NET assemblies compiled for any target framework -- including .NET and .NET Framework.
- Load assemblies from specified file paths.
- Recursively search directories for assemblies.
- Log errors and information during the loading process.
- Utilize
MetadataLoadContextfor inspecting assemblies without loading them into the main application domain. - Extension methods for
Type,PropertyInfo,MemberInfo, andCustomAttributeDatato facilitate inspecting type and attribute metadata. - Get XML documentation comments for types, properties, and members (requires XML documentation files to be present).
- .NET 8.0 SDK
To use SharpMeta in your project, add the following package reference to your .csproj file:
<ItemGroup><PackageReference Include="SharpMeta"/></ItemGroup>Below is an example of how to use the SharpAssemblyResolver class provided by SharpMeta:
usingSystem;usingSystem.IO;usingSystem.Reflection;usingSharpMeta;classProgram{staticvoidMain(){varreferenceFiles=newFileInfo[]{newFileInfo("path/to/your/assembly1.dll"),newFileInfo("path/to/your/assembly2.dll")};varreferenceDirectories=newDirectoryInfo[]{newDirectoryInfo("path/to/your/reference/directory")};usingvarcontext=SharpAssemblyResolver.CreateBuilder().AddReferenceFiles(referenceFiles).AddReferenceDirectories(referenceDirectories).ToAssemblyResolver().ToMetadataLoadContext();varassembly=context.LoadFromAssemblyPath("path/to/your/target/assembly.dll");Console.WriteLine($"Loaded Assembly: {assembly.FullName}");// Example usage of extension methodsforeach(vartypeinassembly.GetTypes()){if(type.ImplementsAnyInterface(("System.Collections","IEnumerable"))){Console.WriteLine($"{type.FullName} implements IEnumerable");}if(type.IsProbableDictionary(outvarkeyType,outvarvalueType)){Console.WriteLine($"{type.FullName} is a probable dictionary with key type {keyType} and value type {valueType}");}foreach(varpropertyintype.GetProperties()){if(property.IsNullable()){Console.WriteLine($"{property.Name} is nullable");}}foreach(varmemberintype.GetMembers()){if(member.TryGetCustomAttributeData<ObsoleteAttribute>(outvarattributeData)){Console.WriteLine($"{member.Name} has ObsoleteAttribute with message: {attributeData.GetNamedArgument<string>("Message")}");}}}}}Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss any changes. See CONTRIBUTING.
This project is licensed under the MIT License. See the LICENSE file for details.