pamidur / aspect-injector

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

Cannot use aspect created in separate C# project in VB.NET project #174

Closed Meigs2 closed 2 years ago

Meigs2 commented 2 years ago

You can create a aspect in one C# library project A, then use it in another C# console app B. However, you cannot use the aspect made in A in a VB.NET console app C.

Steps to reproduce:

  1. Create 3 .Net Framework 4.7.2 projects, one C# class library, one C# console app and a VB.NET console app.
  2. Reference AspectInjector in all 3 apps
  3. Add the following class to the C# class library:

    [Aspect(Scope.Global)]
    [Injection(typeof(TraceAspectAttribute))]
    public class TraceAspectAttribute : Attribute
    {
        [Advice(Kind.Before, Targets = Target.Method)]
        public void TraceStart(
            [Argument(Source.Type)] Type type,
            [Argument(Source.Name)] string name)
        {
            Console.WriteLine($"[{DateTime.UtcNow}] Method {type.Name}.{name} started");
        }
    
        [Advice(Kind.After, Targets = Target.Method)]
        public void TraceFinish(
            [Argument(Source.Type)] Type type,
            [Argument(Source.Name)] string name)
        {
            Console.WriteLine($"[{DateTime.UtcNow}] Method {type.Name}.{name} finished");
        }
    }
  4. Reference the C# class library in both console apps
  5. In the C# console app, add [TraceAspect] to the Program class containing main, then start the app. The console will include a log stating Main started, as expected.
  6. In the VB.NET console app, add <TraceAspect> to the Module containing main, then start the app. The build will fail with the following output:
    1>AspectInjector|2.6.1 : error AI_ERR0: Processing failure: System.NullReferenceException: Object reference not set to an instance of an object.
    1>     at AspectInjector.Core.Services.InjectionReader.FindInjections(TypeDefinition type)
    1>     at AspectInjector.Core.Services.InjectionReader.<>c.<ExtractInjections>b__4_0(CustomAttribute a)
    1>     at System.Linq.Enumerable.SelectIListIterator`2.MoveNext()
    1>     at System.Linq.Enumerable.WhereEnumerableIterator`1.ToArray()
    1>     at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
    1>     at AspectInjector.Core.Services.InjectionReader.ExtractInjections(ICustomAttributeProvider target)
    1>     at AspectInjector.Core.Services.InjectionReader.ReadAll(AssemblyDefinition assembly)
    1>     at AspectInjector.Core.Processor.PatchAssembly(AssemblyDefinition assembly, Boolean optimize, Boolean verbose)
    1>     at FluentIL.PatcherBase.Process(String assemblyFile, IAssemblyResolver resolver, Boolean optimize, Boolean verbose)
    1>     at AspectInjector.Compiler.Execute(String filename, IReadOnlyList`1 references, Boolean optimize, Boolean verbose). Please submit an issue to https://github.com/pamidur/aspect-injector
    1>AspectInjector : error AI_FAIL: Aspect Injector processing has failed. See other errors.

    The same as #173.

pamidur commented 2 years ago

Hi, @Meigs2 , thank you for the report, I'll take a look asap

pamidur commented 2 years ago

seem to be fixed in 2.7.3-pre1