InterceptorTemplate by Andrei Ignat
Nuget / site data
Details
Info
Name: InterceptorTemplate
Package Description
Author: Andrei Ignat
NuGet: https://www.nuget.org/packages/RSCG_InterceptorTemplate/
You can find more details at https://github.com/ignatandrei/RSCG_InterceptorTemplate
Source : https://github.com/ignatandrei/RSCG_InterceptorTemplate
Original Readme
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 ( see templates at src/RSCG_InterceptorTemplateConsole/Interceptors/ , start with GenericInterceptorForAllMethods.txt )
```xml
<ItemGroup>
<!-- <AdditionalFiles Include="Interceptors\TestFullNameWithArguments.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AdditionalFiles> -->
<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!
About
Andrei Ignat
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- FullName.txt
- TestFullNameWithArguments.txt
- GenericInterceptorForAllMethods.txt
- IPersonLoader.cs
This is the CSharp Project that references InterceptorTemplate
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="Interceptors\TestFullNameWithArguments.txt">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AdditionalFiles>
<AdditionalFiles Include="Interceptors\GenericInterceptorForAllMethods.txt">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AdditionalFiles>
<AdditionalFiles Include="Interceptors\FullName.txt">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="RSCG_InterceptorTemplate" Version="8.2023.2811.446" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RSCG_DemoObjects\RSCG_DemoObjects.csproj" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);RSCG_InterceptorTemplate</InterceptorsPreviewNamespaces>
</PropertyGroup>
<PropertyGroup>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
This is the use of InterceptorTemplate in Program.cs
Console.WriteLine("Run the compile.ps1 file!");
var p=new Person();
p.FirstName="Andrei";
p.LastName="Ignat";
Console.WriteLine("debug for "+p.FullName());
Console.WriteLine("this is "+p.FullName());
var x = p.Test();
Console.WriteLine(x);
var newPerson = new Person();
newPerson.FirstName = "Andrei";
newPerson.LastName = "Ignat";
var namePerson = newPerson.FullName();
Console.WriteLine(namePerson);
Console.WriteLine("loaded "+Person.PersonsLoaded());
Console.WriteLine("loaded " + RSCG_DemoObjects.Person.PersonsLoaded());
Console.WriteLine("and again " + RSCG_DemoObjects.Person.PersonsLoaded());
Console.WriteLine("and now with argument " + newPerson.TestFullNameWithArguments("<","!+",">",2));
Console.WriteLine("and a random person " + Person.ShowRandomPersonNumber(1));
var q= await PersonLoader.SavePerson(newPerson);
PersonLoader.Connect();
//Console.ReadLine();
IPersonLoader ipl=new PersonLoader();
await ipl.InsertPerson(newPerson);
This is the use of InterceptorTemplate in FullName.txt
//example generating for full name {{Version}}
#pragma warning disable CS1591
#pragma warning disable CS9113
namespace System.Runtime.CompilerServices{
[AttributeUsage(AttributeTargets.Method,AllowMultiple =true)]
file class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
{
}
}//end namespace
namespace RSCG_InterceptorTemplate{
static partial class SimpleIntercept
{
{{ for loc in ser.dataForEachIntercepts }}
//replace code:{{loc.code}}";
//replace code:{{loc.CodeNumbered}}";
[System.Runtime.CompilerServices.InterceptsLocation(@"{{loc.Path}}", {{loc.Line}}, {{loc.StartMethod}})]
{{ end }}
//[System.Diagnostics.DebuggerStepThrough()]
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.DarkGreen;
Console.WriteLine("start specific FullName template-->{{ser.item.MethodSignature}}");
{{ser.item.ReturnString}} {{(ser.item.HasTaskReturnType ? "await" : "")}} {{ser.item.CallMethod}};
}
finally{
Console.WriteLine("end specific template-->{{ser.item.MethodSignature}}");
Console.BackgroundColor = cc;
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
This is the use of InterceptorTemplate in TestFullNameWithArguments.txt
//example generating for TestFullNameWithArguments {{Version}}
#pragma warning disable CS1591
#pragma warning disable CS9113
namespace System.Runtime.CompilerServices{
[AttributeUsage(AttributeTargets.Method,AllowMultiple =true)]
file class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
{
}
}//end namespace
namespace RSCG_InterceptorTemplate{
static partial class SimpleIntercept
{
{{ for loc in ser.dataForEachIntercepts }}
//replace code:{{loc.code}}";
//replace code:{{loc.CodeNumbered}}";
[System.Runtime.CompilerServices.InterceptsLocation(@"{{loc.Path}}", {{loc.Line}}, {{loc.StartMethod}})]
{{ end }}
//[System.Diagnostics.DebuggerStepThrough()]
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;
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
This is the use of InterceptorTemplate in GenericInterceptorForAllMethods.txt
//example generating generic for all methods in a class {{Version}}
#pragma warning disable CS1591
#pragma warning disable CS9113
namespace System.Runtime.CompilerServices{
[AttributeUsage(AttributeTargets.Method,AllowMultiple =true)]
file class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
{
}
}//end namespace
namespace RSCG_InterceptorTemplate{
static partial class SimpleIntercept
{
{{ for loc in ser.dataForEachIntercepts }}
//replace code:{{loc.code}}";
//replace code:{{loc.CodeNumbered}}";
[System.Runtime.CompilerServices.InterceptsLocation(@"{{loc.Path}}", {{loc.Line}}, {{loc.StartMethod}})]
{{ end }}
//[System.Diagnostics.DebuggerStepThrough()]
public static {{(ser.item.HasTaskReturnType?"async":"")}} {{ser.item.TypeReturn}} {{ser.item.MethodSignature}}({{ser.item.ThisArgument}} {{ser.item.ArgumentsForCallMethod}} )
{
try{
Console.WriteLine("start from generic template-->{{ser.item.MethodSignature}}");
{{ser.item.ReturnString}} {{(ser.item.HasTaskReturnType ? "await" : "")}} {{ser.item.CallMethod}};
}
finally{
Console.WriteLine("end from generic template-->{{ser.item.MethodSignature}}");
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
This is the use of InterceptorTemplate in IPersonLoader.cs
namespace RSCG_DemoObjects;
public interface IPersonLoader
{
Task<Person> InsertPerson(Person p);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
Usefull
Download Example (.NET C# )
Share InterceptorTemplate
https://ignatandrei.github.io/RSCG_Examples/v2/docs/InterceptorTemplate