Skip to content

sunriax/reflection

Repository files navigation

Version: 1.0 Release NuGet Build Status codecov License: 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

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

Initialize with directory

Reflection r = new Reflection("Path to dll files", "Ending of filenames");

Initialize with filenames

string[] files = { "file1.dll", "file2.dll" };

Reflection r = new Reflection(files);

Parameter

ConfigPath

Path to config file

Reflection r = new Reflection("ReflectionLib.json", "...");

Section

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

LibraryPath

Path to DLL files.

Reflection r = new Reflection(@"Reflection", "...");

FileSpecifier

Description of file ending.

Reflection r = new Reflection("...", "*ReflectorLib.dll");

LibraryFile

Include path for predefined libraries

string[] files = { "file1.dll", "file2.dll" };

Reflection r = new Reflection(files);

Load instance by property

With abstract class

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

With interface

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

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

public abstract class AbstractReflector
{
    public abstract string Key { get; }
    public abstract string Message();
}

ConcreteReflector.cs

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

With an interface

IReflector.cs

public interface IReflector
{
    string Key { get; }
    string Message();
}

ConcreteReflector.cs

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

Exception handling

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