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.
Reflectionr=newReflection("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"]}]}Reflectionr=newReflection("Path to dll files","Ending of filenames");string[]files={"file1.dll","file2.dll"};Reflectionr=newReflection(files);Path to config file
Reflectionr=newReflection("ReflectionLib.json","...");Select section in ?.json file
Reflectionr=newReflection("...",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.
Reflectionr=newReflection(@"Reflection","...");Description of file ending.
Reflectionr=newReflection("...","*ReflectorLib.dll");Include path for predefined libraries
string[]files={"file1.dll","file2.dll"};Reflectionr=newReflection(files);staticvoidMain(string[]args){Reflectionr=newReflection(@"Reflection","*ReflectorLib.dll");// Use standard constructor of classAbstractReflectora1=r.GetInstanceByProperty<AbstractReflector>(nameof(a1.Key),"Lib1");// Inject data into constructor of classAbstractReflectora2=r.GetInstanceByProperty<AbstractReflector>(nameof(a2.Key),"Lib1",newobject[]{"Injected constructor parameter"});// Call functionsa1.?a2.?}staticvoidMain(string[]args){Reflectionr=newReflection(@"IReflection","*IReflectorLib.dll");// Use standard constructor of classIReflectora1=r.GetInstanceByProperty<IReflector>(nameof(a1.Key),"Lib1");// Inject data into constructor of classIReflectora2=r.GetInstanceByProperty<IReflector>(nameof(a2.Key),"Lib1",newobject[]{"Injected constructor parameter"});// Call functionsa1.?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.
publicabstractclassAbstractReflector{publicabstractstringKey{get;}publicabstractstringMessage();}publicclassConcreteReflector:AbstractReflector{privatereadonlystringmessage;// If the empty constructor should not be visible,// it is possible to make it private//private ConcreteReflector()//{// this.message = "Constructor without parameter";//}publicConcreteReflector(){this.message="Constructor without parameter";}publicConcreteReflector(stringmessage){this.message=message;}privateconststringkey="Lib1";publicoverridestringKey{get=>key;}publicoverridestringMessage(){returnmessage;}}publicinterfaceIReflector{stringKey{get;}stringMessage();}publicclassConcreteIReflector:IReflector{privatereadonlystringmessage;// If the empty constructor should not be visible,// it is possible to make it private//private ConcreteIReflector()//{// this.message = "Constructor without parameter";//}publicConcreteIReflector(){this.message="Constructor without parameter";}publicConcreteIReflector(stringmessage){this.message=message;}privateconststringkey="Lib1";publicstringKey{get=>key;}publicstringMessage(){returnthis.message;}}All libraries in the RaGae.* namespace are using the RaGae.Exception model. The model implements an error code and message.
staticvoidMain(string[]args){try{Reflectionr5=newReflection(@"Reflection","*ReflectorLib.dll");// ...}catch(ReflectionExceptionex){Console.WriteLine(ex.ErrorCode);Console.WriteLine(ex.Message);Console.WriteLine(ex.ErrorMessage());}catch(Exceptionex){Console.WriteLine(ex.Message);}}R. GÄCHTER