pamidur / aspect-injector

AOP framework for .NET (c#, vb, etc)
Apache License 2.0
745 stars 112 forks source link

Runtime Aspect Loader feature #127

Open nicewk opened 4 years ago

nicewk commented 4 years ago

Great job! It could be better if the Aspect attribute can add to the target automatically according to a config file.

pamidur commented 4 years ago

Hi! Thanks! I'm actually working on something like this. But the idea is not the config file but kinda fluent loader. So you'll be able to do something like:

var assembly = AspectLoader.Load(filename, loader => {
                loader.Inject(typeof(LogAspect)).IntoMethod(m => m.Attributes == MethodAttributes.Public);
            });

This might be useful if don't own target assembly. E.g. test loader or custom application loader/partcher.

If you instead own target assembly you can use Propagate feature like this:

    [Aspect(Scope.Global)]
    [Injection(typeof(LogAspect), Propagation = PropagateTo.Types | PropagateTo.Methods, PropagationFilter = "^MyMethod")]
    public class LogAspect : Attribute
    {
        [Advice(Kind.Before, Targets = Target.Method | Target.Public)]
        public void Log([Argument(Source.Name)] string method)
        {
            Console.WriteLine($"Entering {method}");
        }
    }

And then apply it to assembly

[assembly:LogAspect]

Let me know if it works for you, and how would you like it work otherwise. Your usage scenarios would be plus)

nicewk commented 4 years ago

Thanks @pamidur. That makes sense. I will try it.

emisand commented 3 years ago

@pamidur Is the AspectLoader or Propagation features already implemented? This is the kind of feature I need to apply aspects to classes that implement an interface from a framework I'm working on. I would need to implement some reflection filters to check if a method is within a class with some specific interfaces or attributes in order to apply the aspect.

pamidur commented 3 years ago

Hi @emisand , unfortunately AspectLoader is not ready (not even close to ready as there is some technical difficulties) There is another feature though that might be of some interest to you - Interface triggers which allows you to have some injection based on implemented interface - see here https://github.com/pamidur/aspect-injector/releases/tag/2.5.0