Can I get parameter names/values procedurally from the currently executing function?

后端 未结 6 1383
猫巷女王i
猫巷女王i 2020-12-02 14:38

I would like to do something like this:

public MyFunction(int integerParameter, string stringParameter){
    //Do this:
    LogParameters();
    //Instead of         


        
6条回答
  •  旧时难觅i
    2020-12-02 14:49

    Unless you use the debugger API, you cannot loop through parameter values of a different method on the call stack. Though you can get the parameter names from the callstack (as others have mentioned).

    The closest thing would be:

    public MyFunction(int integerParameter, string stringParameter){
        LogParameters(integerParameter, stringParameter);
    }
    
    public void LogParameters(params object[] values){
        // Get the parameter names from callstack and log names/values
    }
    

提交回复
热议问题