vendredi 7 juillet 2017

Automatically passing own type to function call

I'd like to pass a value when calling a function but want to omit to actually add it as a parameter.

To be more precise I'd like to write a logger that also prints which class called the logging function but don't want to always pass a "this" as a parameter.

Example code:

class static Logger{
    public static void LogMsg(string msg, object objectCalling){
         Print(objectCalling.GetType().Name + ": " + msg);
    }
    private void Print(string msg){
        // print it
    } 
}

class SomeClass{
    private void WriteTestLog() {
        Logger.LogMsg("Testing!");
    }
}

This should then create an output like: "SomeClass: Testing!"

I am not sure how to tackle this maybe I am just missing sth.





Aucun commentaire:

Enregistrer un commentaire