Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ignatandrei/RSCG_InterceptorTemplate
Interceptor templating
https://github.com/ignatandrei/RSCG_InterceptorTemplate
Last synced: about 1 month ago
JSON representation
Interceptor templating
- Host: GitHub
- URL: https://github.com/ignatandrei/RSCG_InterceptorTemplate
- Owner: ignatandrei
- License: mit
- Created: 2023-11-23T18:47:10.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-03T22:34:28.000Z (3 months ago)
- Last Synced: 2024-11-03T16:07:00.062Z (about 1 month ago)
- Language: C#
- Size: 87.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - https://github.com/ignatandrei/RSCG_InterceptorTemplate
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:
```csharp
Console.WriteLine("and now with argument " + newPerson.TestFullNameWithArguments("<","!+",">",2));
```it can intercept it with the arguments .
For example, if you use this template
```csharp
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:
```csharp
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
```xml
true
$(BaseIntermediateOutputPath)\GX
$(InterceptorsPreviewNamespaces);RSCG_InterceptorTemplate```
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 )
```csharp
```xml
PreserveNewest
```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
```powershell
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!
# More Roslyn Source Code Generators
You can find more RSCG with examples at [Roslyn Source Code Generators](https://ignatandrei.github.io/RSCG_Examples/v2/)