samedi 18 septembre 2021

Is it possible to create generic overrides using Reflections?

We're currently trying to figure out how to create really extended logging. so including the method call stack, method parameters, values, everything.

to do that we're basically using reflections. we have a factory method creating an instance and then comes the dirty point. we're creating this instance using string concatenated c# code.

the target is to append the logging code before and after the executed function in the context while having access to the type signature

i personally would rather like something like this:

public class LoggingWrapper<T> : T{
    public LoggingWrapper(){
        var virtualMethodsOfT = /* Do Reflection magic here */;
        foreach(var method in virtualMethodsOfT){
            this.CreateOverrideFunction(method.Name, ()=> LoggingOutline(method.BaseCall));
        }
    }

    static LoggingOutline(Action baseMethod){
    // Logging Code before
    baseMethod();
    // Logging Code after
    }
}

to make it look sexy i'd probably also store the reflection data in a in-memory dictionary, because i heard it's awfully slow





Aucun commentaire:

Enregistrer un commentaire