Git Product home page Git Product logo

reflection's Introduction

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

reflection's People

Contributors

0x007e avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.