RSCG_InterceptorTemplate 8.2023.2811.524
dotnet add package RSCG_InterceptorTemplate --version 8.2023.2811.524
NuGet\Install-Package RSCG_InterceptorTemplate -Version 8.2023.2811.524
<PackageReference Include="RSCG_InterceptorTemplate" Version="8.2023.2811.524" />
paket add RSCG_InterceptorTemplate --version 8.2023.2811.524
#r "nuget: RSCG_InterceptorTemplate, 8.2023.2811.524"
// Install RSCG_InterceptorTemplate as a Cake Addin #addin nuget:?package=RSCG_InterceptorTemplate&version=8.2023.2811.524 // Install RSCG_InterceptorTemplate as a Cake Tool #tool nuget:?package=RSCG_InterceptorTemplate&version=8.2023.2811.524
RSCG_InterceptorTemplate
Interceptor template - supported from >= .NET 8.0 . Uses also experimental interceptor feature from C#12.0
It can rewrite any method call to any other method call. It can also rewrite any method call to any other method call with the same signature. It can also rewrite any method call to any other method call with the same signature and the same return type.
It does not ( yet ) support generic methods.
For example, if you have a method call like this:
Console.WriteLine("and now with argument " + newPerson.TestFullNameWithArguments("<","!+",">",2));
it can intercept it with the arguments . For example, if you use this template
public static {{(ser.item.HasTaskReturnType?"async":"")}} {{ser.item.TypeReturn}} {{ser.item.MethodSignature}}({{ser.item.ThisArgument}} {{ser.item.ArgumentsForCallMethod}} )
{
var cc=Console.BackgroundColor ;
try{
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine("start specific TestFullNameWithArguments template-->{{ser.item.MethodSignature}}");
Console.WriteLine("number of arguments = {{ser.item.Arguments.size}}");
{{ for argum in ser.item.Arguments }}
Console.WriteLine("argument {{for.index+1}} type {{argum.Type}} and value = "+ {{argum.Name}});
{{ end }}
{{ser.item.ReturnString}} {{(ser.item.HasTaskReturnType ? "await" : "")}} {{ser.item.CallMethod}};
}
finally{
Console.WriteLine("end specific template-->{{ser.item.MethodSignature}}");
Console.BackgroundColor = cc;
}
}
The final result will be:
public static string Intercept_newPerson_TestFullNameWithArguments(this RSCG_DemoObjects.Person newPerson ,string start,string separator,string end,int repeat )
{
var cc=Console.BackgroundColor ;
try{
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine("start specific TestFullNameWithArguments template-->Intercept_newPerson_TestFullNameWithArguments");
Console.WriteLine("number of arguments = 4");
Console.WriteLine("argument 1 type string and value = "+ start);
Console.WriteLine("argument 2 type string and value = "+ separator);
Console.WriteLine("argument 3 type string and value = "+ end);
Console.WriteLine("argument 4 type int and value = "+ repeat);
return newPerson.TestFullNameWithArguments(start,separator,end,repeat);
}
finally{
Console.WriteLine("end specific template-->Intercept_newPerson_TestFullNameWithArguments");
Console.BackgroundColor = cc;
}
}
You can use any template . Some examples at src/RSCG_InterceptorTemplateConsole/Interceptors
How to use it
Add to your project (>= .NET 8 ) the nuget package RSCG_InterceptorTemplate
<ItemGroup>
<PackageReference Include="RSCG_InterceptorTemplate" Version="8.2023.2811.524" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);RSCG_InterceptorTemplate</InterceptorsPreviewNamespaces>
</PropertyGroup>
Make a folder Interceptors in the project and add also at least the generic interceptor
<ItemGroup>
<AdditionalFiles Include="Interceptors\GenericInterceptorForAllMethods.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AdditionalFiles>
</ItemGroup>
The interceptor will not run template at build time in Visual Studiom, but it will run at build time in dotnet build.
For this, you need to have something like that ( powershell file) -I named mine compile.ps1
cls
#not necessary for CI builds, but only for debugging purposes
Write-Host "delete obj and bin"
gci obj -recurse | foreach{ri $_.FullName -recurse -force }
gci bin -recurse | foreach{ri $_.FullName -recurse -force }
#for windows batch file
#setx InterceptMethods "FullName"
#echo Environment variable InterceptMethods has been set to %InterceptMethods%
#put here the names of the methods you want to intercept , separated by ;
$env:InterceptMethods = "FullName;Test;PersonsLoaded;TestFullNameWithArguments;ShowRandomPersonNumber;Connect;SavePerson;InsertPerson"
Write-Host "Environment variable $env:InterceptMethods has been set to " $env:InterceptMethods
dotnet clean
dotnet restore
dotnet build /p:EmitCompilerGeneratedFiles=true --disable-build-servers --force
#debug only
# dotnet run --project RSCG_InterceptorTemplateConsole/RSCG_InterceptorTemplateConsole.csproj
Enjoy!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.8.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
8.2023.2811.524 | 2,879 | 11/28/2023 |
8.2023.2811.446 | 148 | 11/28/2023 |
1.0.0 | 152 | 11/27/2023 |