GenericSharpLoading 1.0.0
dotnet add package GenericSharpLoading --version 1.0.0
NuGet\Install-Package GenericSharpLoading -Version 1.0.0
<PackageReference Include="GenericSharpLoading" Version="1.0.0" />
paket add GenericSharpLoading --version 1.0.0
#r "nuget: GenericSharpLoading, 1.0.0"
// Install GenericSharpLoading as a Cake Addin #addin nuget:?package=GenericSharpLoading&version=1.0.0 // Install GenericSharpLoading as a Cake Tool #tool nuget:?package=GenericSharpLoading&version=1.0.0
GenericSharpLoading
GenericSharpLoading is a .NET Standard 2.0 compatible library to dynamically load external .NET libraries into the runtime.
Usage
Basic
If we just want to grab all classes from an assembly, we can just load the assembly and call .GetInstances
var loader = new SharpLoader();
loader.LoadAssembly(@"C:\Foo\Bar\Apple.dll");
IEnumerable<IFruit> fruits = loader.GetInstances<IFruit>(/* constructor args */);
Continuously load new types
If we instead want to be able to load assemblies from a folder and update our runtime if new assemblies were added, a new loader can be spawned which is connected to our "root" loader and caches which types were already initialized
var loader = new SharpLoader();
ConfiguredSharpLoader<IFruit> fruitLoader = loader.ForType<IFruit>();
List<IFruit> fruits = new List<IFruit>();
void LoadNewFruits()
{
//.LoadAssemblies loads all .exe and .dll files in the
//given folder, but each file is only loaded once
loader.LoadAssemblies(@"C:\Foo\Bar\Fruits\");
//.GetNewInstances will initialize each type only once whilst
//.GetInstances will create new instances every time it's called
var newFruits = fruitLoader.GetNewInstances(/* constructor args */);
//var allFruits = fruitLoader.GetInstances(/* constructor args */);
fruits.AddRange(newFruits);
}
Instantiate types from already loaded assemblies
If we already have plugins loaded in the runtime, because they are included in the the main assembly which is currently executed, we obviously cannot load our own assembly using .LoadAssembly("Path/To/Our/Assembly.exe")
.
For that case, you can pass a reference to an Assembly already known to the runtime to .LoadAssembly
and the SharpLoader adds it to its assembly cache
class FruitManager
{
void LoadFruits()
{
var loader = new SharpLoader();
loader.LoadAssembly(typeof(FruitManager).Assembly);
IEnumerable<IFruit> fruits = loader.GetInstances<IFruit>(/* constructor args */);
}
}
Logging
GenericSharpLoading provides a logging interface so the main application can log actions done by this library.
It is as simple as the static class GenericSharpLoading
provides the event OnLog
which is fired every time the library would log something.
Along with the message a log level is given, which is just a string containing the word DEBUG
, INFO
, WARNING
or ERROR
. These strings will probably always be the same, but if you want to process it, I would recommend to use the class GenericSharpLoading.LogLevel
(nested class) and its four constants DEBUG
, INFO
, WARNING
or ERROR
which names are guaranteed to be never changed.
Example
class FruitManager
{
void Init()
{
GenericSharpLoading.GenericSharpLoading.OnLog += this.Log;
LoadFruits();
}
void LoadFruits()
{
var loader = new PluginLoader();
//...
}
void Log(string message, string logLevel)
{
Console.WriteLine($"[FruitManager][{logLevel}] {message}");
}
}
License
GenericSharpLoading is licensed under the MIT License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 1,017 | 5/6/2018 |