RSCG – WhatIAmDoing
Intercept any method in any project
This is how you can use WhatIAmDoing .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!-- <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" /> -->
<PackageReference Include="RSCG_WhatIAmDoing" Version="8.2024.10201.735" />
<PackageReference Include="RSCG_WhatIAmDoing_Common" Version="8.2024.10201.735" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);RSCG_InterceptorTemplate</InterceptorsPreviewNamespaces>
</PropertyGroup>
</Project>
The code that you will use is
using RSCG_WhatIAmDoing_Common;
Console.WriteLine("Hello, World!");
var data = CachingData.Methods().ToArray();
foreach (var item in data)
{
Console.WriteLine($"Method {item.typeAndMethodData.MethodName} from class {item.typeAndMethodData.TypeOfClass} Time: {item.StartedAtDate} state {item.State} ");
Console.WriteLine($" =>Arguments: {item.ArgumentsAsString()}");
if ((item.State & AccumulatedStateMethod.HasResult) == AccumulatedStateMethod.HasResult)
{
Console.WriteLine($" =>Result: {item.Result}");
}
}
using RSCG_WhatIAmDoing;
using RSCG_WhatIAmDoing_Common;
namespace WIADDemo;
//[ExposeClass(typeof(Encoding), nameof(Encoding.EncodingName))]
[InterceptStatic("System.Console.*")] // regex
internal class InterceptorMethodStatic : InterceptorMethodStaticBase, IInterceptorMethodStatic
{
}
The code that is generated is
#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
{
//replace code:Console.WriteLine("Hello, World!");
//replace code:123456789!123456789!123456789!12345
[System.Runtime.CompilerServices.InterceptsLocation(@"D:\gth\RSCG_Examples\v2\rscg_examples\WIAD\src\WIADDemo\WIADDemo\Program.cs", 3, 9)]
//[System.Diagnostics.DebuggerStepThrough()]
public static void Intercept__WriteLine_0 ( string? value )
{
System.Collections.Generic.Dictionary<string,string?> valValues = new (){
};//end valValues
System.Collections.Generic.Dictionary<string,string?> stringValues = new() {
{ "value", value ?.ToString() } ,
};//end stringValues
System.Collections.Generic.Dictionary<string,string?>
expValues = new() {
};//end exposeValues
var x=WIADDemo.InterceptorMethodStatic .InterceptStaticMethodBefore(
$$$""""
{"IsVoid":true,"Tag":"","MethodName":"WriteLine","TypeOfClass":"System.Console"}
""""
,valValues , stringValues , expValues
)
;
try{
System.Console.WriteLine(value);
WIADDemo.InterceptorMethodStatic .InterceptMethodAfterWithoutResult(x);
}
catch(System.Exception ex){
WIADDemo.InterceptorMethodStatic .InterceptMethodException(x,ex);
throw;
}
finally{
WIADDemo.InterceptorMethodStatic .InterceptMethodFinally(x);
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
#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
{
//replace code: Console.WriteLine($"Method {item.typeAndMethodData.MethodName} from class {item.typeAndMethodData.TypeOfClass} Time: {item.StartedAtDate} state {item.State} ");
//replace code:123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!1234
[System.Runtime.CompilerServices.InterceptsLocation(@"D:\gth\RSCG_Examples\v2\rscg_examples\WIAD\src\WIADDemo\WIADDemo\Program.cs", 8, 13)]
//[System.Diagnostics.DebuggerStepThrough()]
public static void Intercept__WriteLine_1 ( string? value )
{
System.Collections.Generic.Dictionary<string,string?> valValues = new (){
};//end valValues
System.Collections.Generic.Dictionary<string,string?> stringValues = new() {
{ "value", value ?.ToString() } ,
};//end stringValues
System.Collections.Generic.Dictionary<string,string?>
expValues = new() {
};//end exposeValues
var x=WIADDemo.InterceptorMethodStatic .InterceptStaticMethodBefore(
$$$""""
{"IsVoid":true,"Tag":"","MethodName":"WriteLine","TypeOfClass":"System.Console"}
""""
,valValues , stringValues , expValues
)
;
try{
System.Console.WriteLine(value);
WIADDemo.InterceptorMethodStatic .InterceptMethodAfterWithoutResult(x);
}
catch(System.Exception ex){
WIADDemo.InterceptorMethodStatic .InterceptMethodException(x,ex);
throw;
}
finally{
WIADDemo.InterceptorMethodStatic .InterceptMethodFinally(x);
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
#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
{
//replace code: Console.WriteLine($" =>Arguments: {item.ArgumentsAsString()}");
//replace code:123456789!123456789!123456789!123456789!123456789!123456789!12345678
[System.Runtime.CompilerServices.InterceptsLocation(@"D:\gth\RSCG_Examples\v2\rscg_examples\WIAD\src\WIADDemo\WIADDemo\Program.cs", 9, 13)]
//[System.Diagnostics.DebuggerStepThrough()]
public static void Intercept__WriteLine_2 ( string? value )
{
System.Collections.Generic.Dictionary<string,string?> valValues = new (){
};//end valValues
System.Collections.Generic.Dictionary<string,string?> stringValues = new() {
{ "value", value ?.ToString() } ,
};//end stringValues
System.Collections.Generic.Dictionary<string,string?>
expValues = new() {
};//end exposeValues
var x=WIADDemo.InterceptorMethodStatic .InterceptStaticMethodBefore(
$$$""""
{"IsVoid":true,"Tag":"","MethodName":"WriteLine","TypeOfClass":"System.Console"}
""""
,valValues , stringValues , expValues
)
;
try{
System.Console.WriteLine(value);
WIADDemo.InterceptorMethodStatic .InterceptMethodAfterWithoutResult(x);
}
catch(System.Exception ex){
WIADDemo.InterceptorMethodStatic .InterceptMethodException(x,ex);
throw;
}
finally{
WIADDemo.InterceptorMethodStatic .InterceptMethodFinally(x);
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
#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
{
//replace code: Console.WriteLine($" =>Result: {item.Result}");
//replace code:123456789!123456789!123456789!123456789!123456789!123456
[System.Runtime.CompilerServices.InterceptsLocation(@"D:\gth\RSCG_Examples\v2\rscg_examples\WIAD\src\WIADDemo\WIADDemo\Program.cs", 12, 17)]
//[System.Diagnostics.DebuggerStepThrough()]
public static void Intercept__WriteLine_3 ( string? value )
{
System.Collections.Generic.Dictionary<string,string?> valValues = new (){
};//end valValues
System.Collections.Generic.Dictionary<string,string?> stringValues = new() {
{ "value", value ?.ToString() } ,
};//end stringValues
System.Collections.Generic.Dictionary<string,string?>
expValues = new() {
};//end exposeValues
var x=WIADDemo.InterceptorMethodStatic .InterceptStaticMethodBefore(
$$$""""
{"IsVoid":true,"Tag":"","MethodName":"WriteLine","TypeOfClass":"System.Console"}
""""
,valValues , stringValues , expValues
)
;
try{
System.Console.WriteLine(value);
WIADDemo.InterceptorMethodStatic .InterceptMethodAfterWithoutResult(x);
}
catch(System.Exception ex){
WIADDemo.InterceptorMethodStatic .InterceptMethodException(x,ex);
throw;
}
finally{
WIADDemo.InterceptorMethodStatic .InterceptMethodFinally(x);
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/WhatIAmDoing