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:
Create 3 .Net Framework 4.7.2 projects, one C# class library, one C# console app and a VB.NET console app.
Reference AspectInjector in all 3 apps
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");
}
}
Reference the C# class library in both console apps
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.
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.
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:
Add the following class to the C# class library:
[TraceAspect]
to theProgram
class containing main, then start the app. The console will include a log statingMain
started, as expected.<TraceAspect>
to the Module containing main, then start the app. The build will fail with the following output:The same as #173.