NamespaceSystemLibrary.Common.Net SystemLibrary.Common.Net.dll
Class to find types and embedded resources in loaded Assemblies
Built on System.Reflection.Assembly
Assemblies
public static class Assemblies
Inheritance
Examples
Arguments
Methods
Find all types inheriting class T in all loaded assemblies
Skips searching in assemblies starting with common names like: Microsoft, System, Windows, EntityFramework, AWS, Serilog, MSTest, nunit, Newtonsoft, Xamarin, Dapper, Autofac, Automapper, Salesforce, ...
public static IEnumerable<Type> FindAllTypesInheriting<TClassType>() where TClassType : class
public class Car : IVehicle {
}
var vehicles = Assemblies.FindAllTypesInheriting<IVehicle>
// returns 'Car' and all other types that inherits/implements IVehicle
Generic types
Name | Description |
---|---|
TClassType | Must be a class |
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Type> | An IEnumerable of Type |
Find all types inheriting class T with a certain attribute in all loaded assemblies
Skips searching in assemblies starting with common names like: Microsoft, System, Windows, EntityFramework, AWS, Serilog, MSTest, nunit, Newtonsoft, Dapper, Autofac, Automapper, Salesforce, ...
public static IEnumerable<Type> FindAllTypesInheritingWithAttribute<TClassType, TAttributeType>() where TClassType : class where TAttributeType : Attribute
public class NameAttribute : Attribute {
}
[NameAttribute]
public class Car : IVehicle {
}
var vehicles = Assemblies.FindAllTypesInheriting<IVehicle,NameAttribute>
// returns 'Car' and all other types that inherits/implements IVehicle which also contains the attribute
Generic types
Name | Description |
---|---|
TClassType | Must be a class |
TAttributeType | Type of class that inherits Attribute |
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Type> | An IEnumerable of Type |
Read an embedded resource's content as string
public static string GetEmbeddedResource(string relativeFolderPath, string fileName, Assembly assembly = null)
var text = Assemblies.GetEmbeddedResource("Folder/SubFolder/SubFolder2", "json.txt");
Assert.IsTrue(text.Contains("hello world"));
// assume a file in Solution Explorer exists at "~/Folder/SubFolder/SubFolder2/json.txt"
// assume "json.txt" has build action 'Embedded Resource'
// assume "json.txt" contains 'hello world' this is now true
Methods arguments
Type | Name | Description |
---|---|---|
System.String | relativeFolderPath | |
System.String | fileName | |
System.Reflection.Assembly | assembly |
Type | Description |
---|---|
System.String |
Read an embedded resource's content as byte array
public static byte[] GetEmbeddedResourceAsBytes(string relativeFolderPath, string fileName, Assembly assembly = null)
var bytes = Assemblies.GetEmbeddedResource("Folder/SubFolder/SubFolder2", "image.jpg");
// bytes contains now the whole image.jpg as an byte array, assuming image.jpg was marked with the build action 'Embedded Resource'
Methods arguments
Type | Name | Description |
---|---|---|
System.String | relativeFolderPath | |
System.String | fileName | |
System.Reflection.Assembly | assembly |
Type | Description |
---|---|
System.Byte[] |
}