Code Monkey home page Code Monkey logo

aspect-injector's Introduction

image

I have never asked for any donations, but today, I ask you, please, consider donating Ukrainian Army.

You can find official ways to do it here or you can donate to the biggest charity organization here

People need to be alive to create open source projects!





Aspect Injector

Aspect Injector is an attribute-based framework for creating and injecting aspects into your .net assemblies.

Project Status

Nuget Nuget Pre Nuget

GitHub (Pre-)Release Date GitHub last commit

Application Status Samples Status

Download

> dotnet add package AspectInjector

Features

  • Compile-time injection - works with Blazor and AOT
  • Injecting Before, After and Around (wrap) Methods, Constructors, Properties and Events
  • Injecting Interface implementations
  • Supports any project that can reference netstandard2.0 libraries, see here
  • Debugging support
  • Roslyn analyzers for your convenience (only c# currently)
  • Can work DI/IoC frameworks #166

Check out samples and docs

Requirements

  • (semi-optional) Nuget 5.0+ for transient build feature. All modern versions of VS and dotnetsdk have it. (If you still use project.json for some reason - make sure you add AspectInjector to all projects in the solution)
  • (optional) For analyzers to work in VSCode, don't forget to enable "omnisharp.enableRoslynAnalyzers": true

Known Issues / Limitations

  • Unsafe methods are not supported and are silently ignored.

Simple advice

Create an aspect with simple advice:

[Aspect(Scope.Global)]
[Injection(typeof(LogCall))]
public class LogCall : Attribute
{
    [Advice(Kind.Before)] // you can have also After (async-aware), and Around(Wrap/Instead) kinds
    public void LogEnter([Argument(Source.Name)] string name)
    {
        Console.WriteLine($"Calling '{name}' method...");   //you can debug it	
    }
}

Use it:

[LogCall]
public void Calculate() 
{ 
    Console.WriteLine("Calculated");
}

Calculate();

Result:

$ dotnet run
Calling 'Calculate' method...
Calculated

Simple mixin

Create an aspect with mixin:

public interface IInitializable
{
    void Init();
}

[Aspect(Scope.PerInstance)]
[Injection(typeof(Initializable))]
[Mixin(typeof(IInitializable))]
public class Initializable : IInitializable, Attribute
{
    public void Init()
    {
        Console.WriteLine("Initialized!");
    }
}

Use it:

[Initializable]
public class Target
{ 
}

var target = new Target() as IInitializable;
target.Init();

Result:

$ dotnet run
Initialized!

aspect-injector's People

Contributors

jerviscui avatar kondr1 avatar maciejwanat avatar om2804 avatar pamidur avatar pvoshell avatar stefh avatar wssmith avatar yuriyivon avatar zhipu123 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.