C # Instrumentation

What would be the easiest way to make C # code? For tools, I want to embed my own code snippets to collect some dynamic information at runtime.

For example (a star is an unimportant part of the code):

for (int i=0; i<s.Length-2; ++i) { if (*) s = s.Substring(1, s.Length-2); } 

I would like to catch the 0 assigned by i, I increment and assign and call a substring. By catching a method call, I mean that I have information about which method it has, and the values ​​of arguments or similar.

I tried to do this with Roslyn by transferring method calls with my own wrappers, which could: a) intercept the values ​​and store them (for example), b) call the actual method, and c) return this result. The only problem is that this approach is really error-prone and difficult (because there are many different cases to cover).

I wonder if there is some kind of library for this purpose, or if someone knows an easier way to do this. Thanks!

+4
source share

Source: https://habr.com/ru/post/1482250/


All Articles