Skip to content

Repository files navigation

Version: 1.0 ReleaseNuGetBuild StatuscodecovLicense: GPL v3

ReflectionLib

Description:

Loading DLL´s at runtime within a .net Framework/Core application. An example project (MakeReflection) can be found in the repository.


Installation

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.


Structure

Initialize with config

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"]}]}

Initialize with directory

Reflectionr=newReflection("Path to dll files","Ending of filenames");

Initialize with filenames

string[]files={"file1.dll","file2.dll"};Reflectionr=newReflection(files);

Parameter

ConfigPath

Path to config file

Reflectionr=newReflection("ReflectionLib.json","...");

Section

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"}]}

LibraryPath

Path to DLL files.

Reflectionr=newReflection(@"Reflection","...");

FileSpecifier

Description of file ending.

Reflectionr=newReflection("...","*ReflectorLib.dll");

LibraryFile

Include path for predefined libraries

string[]files={"file1.dll","file2.dll"};Reflectionr=newReflection(files);

Load instance by property

With abstract class

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.?}

With interface

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.?}

Build your own class

  1. Create a new VisualStudio .NET Standard class library (??ReflectorLib)
  2. If more than one class of same functionallity will be used, create an abstract class or an interface.

With an abstract class

AbstractReflector.cs

publicabstractclassAbstractReflector{publicabstractstringKey{get;}publicabstractstringMessage();}

ConcreteReflector.cs

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;}}

With an interface

IReflector.cs

publicinterfaceIReflector{stringKey{get;}stringMessage();}

ConcreteReflector.cs

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;}}

Exception handling

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

Releases

Packages

Contributors

Languages