Loading DLL´s at runtime within a .net Framework/Core application. An example project (MakeReflection) can be found in the repository.
To install ReflectionLib it is possible to download library [zip | gzip] or install it via nuget.
PM> Install-Package RaGae.Reflection
After adding/installing the ReflectionLib in a project classes of same base or same type or different classes can be loaded dynamically at runtime.
Reflection r = new Reflection("Path to config", "section number");
ReflectionLib.json
{
"ReflectionConfig": [
{
"ReflectionPath": "Reflection",
"FileSpecifier": "*ReflectorLib.dll",
"Files": [
"Reflection/ConcreteReflectorLib.dll"
]
},
{
"ReflectionPath": "IReflection",
"FileSpecifier": "*IReflectorLib.dll",
"Files": [
"IReflection/ConcreteIReflectorLib.dll"
]
}
]
}
Reflection r = new Reflection("Path to dll files", "Ending of filenames");
string[] files = { "file1.dll", "file2.dll" };
Reflection r = new Reflection(files);
Path to config file
Reflection r = new Reflection("ReflectionLib.json", "...");
Select section in ?.json file
Reflection r = new Reflection("...", 0);
ReflectionLib.Files.json
{
"ReflectionConfig": [
{
"Files": [
"Reflection/ConcreteReflectorLib.dll"
]
},
{
"Files": [
"IReflection/ConcreteIReflectorLib.dll"
]
}
]
}
ReflectionLib.Path.json
{
"ReflectionConfig": [
{
"ReflectionPath": "Reflection",
"FileSpecifier": "*ReflectorLib.dll"
},
{
"ReflectionPath": "IReflection",
"FileSpecifier": "*IReflectorLib.dll"
}
]
}
Path to DLL files.
Reflection r = new Reflection(@"Reflection", "...");
Description of file ending.
Reflection r = new Reflection("...", "*ReflectorLib.dll");
Include path for predefined libraries
string[] files = { "file1.dll", "file2.dll" };
Reflection r = new Reflection(files);
static void Main(string[] args)
{
Reflection r = new Reflection(@"Reflection", "*ReflectorLib.dll");
// Use standard constructor of class
AbstractReflector a1 = r.GetInstanceByProperty<AbstractReflector>(nameof(a1.Key), "Lib1");
// Inject data into constructor of class
AbstractReflector a2 = r.GetInstanceByProperty<AbstractReflector>(nameof(a2.Key), "Lib1", new object[] { "Injected constructor parameter" });
// Call functions
a1.?
a2.?
}
static void Main(string[] args)
{
Reflection r = new Reflection(@"IReflection", "*IReflectorLib.dll");
// Use standard constructor of class
IReflector a1 = r.GetInstanceByProperty<IReflector>(nameof(a1.Key), "Lib1");
// Inject data into constructor of class
IReflector a2 = r.GetInstanceByProperty<IReflector>(nameof(a2.Key), "Lib1", new object[] { "Injected constructor parameter" });
// Call functions
a1.?
a2.?
}
- Create a new VisualStudio .NET Standard class library (??ReflectorLib)
- If more than one class of same functionallity will be used, create an abstract class or an interface.
public abstract class AbstractReflector
{
public abstract string Key { get; }
public abstract string Message();
}
public class ConcreteReflector : AbstractReflector
{
private readonly string message;
// If the empty constructor should not be visible,
// it is possible to make it private
//private ConcreteReflector()
//{
// this.message = "Constructor without parameter";
//}
public ConcreteReflector()
{
this.message = "Constructor without parameter";
}
public ConcreteReflector(string message)
{
this.message = message;
}
private const string key = "Lib1";
public override string Key { get => key; }
public override string Message()
{
return message;
}
}
public interface IReflector
{
string Key { get; }
string Message();
}
public class ConcreteIReflector : IReflector
{
private readonly string message;
// If the empty constructor should not be visible,
// it is possible to make it private
//private ConcreteIReflector()
//{
// this.message = "Constructor without parameter";
//}
public ConcreteIReflector()
{
this.message = "Constructor without parameter";
}
public ConcreteIReflector(string message)
{
this.message = message;
}
private const string key = "Lib1";
public string Key { get => key; }
public string Message()
{
return this.message;
}
}
All libraries in the RaGae.* namespace are using the RaGae.Exception model. The model implements an error code and message.
static void Main(string[] args)
{
try
{
Reflection r5 = new Reflection(@"Reflection", "*ReflectorLib.dll");
// ...
}
catch (ReflectionException ex)
{
Console.WriteLine(ex.ErrorCode);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ErrorMessage());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
R. GÄCHTER