开发者

C# (.NET 3.5) Is there any way to get this function name?

开发者 https://www.devze.com 2022-12-13 08:25 出处:网络
I have a function that wraps a call to one of my socket types. If there is an error, I want to be able to print a warning and retry. In the warning, I want to have the method name. However, it was dec

I have a function that wraps a call to one of my socket types. If there is an error, I want to be able to print a warning and retry. In the warning, I want to have the method name. However, it was declared as a lambda. Is this even possible?

How I call the function (a开发者_开发百科ssume in function called myMain):

SafeSocketCommand(() => this.mySocket.ReadCurrentBuffer());

Basic wrapping function:

protected TResult SafeSocketCommand<TResult>(Func<TResult> socketCommand)
{
    TResult retValue = default(TResult);
    try
    {
        retValue = socketCommand();
    }
    catch (PacketLost)
    {
        ReportToLogs("Timeout on command '" + socketCommand.Method.Name);
    }
    return retValue;
}

But socketCommand.Method.Name gives me the calling method (from the Stack Trace?) '< myMain >b__3' and I want the actual function being invoked by socketCommand (mySocket.ReadCurrentBuffer). Is it possible to get this information anywhere, or is it lost due to declaring in a lambda?

EDIT:

I should have mentioned that I use this particular calling convention so that I can use socket based commands of various signatures.

int i = SafeSocketCommand(() => this.mySocket.FunctionReturnsInt())
bool b = SafeSocketCommand(() => this.mySocket.FunctionReturnsBool(string s))
object o = SafeSocketCommand(() => this.mySocket.Complicated(string s, int i, bool b))

It also handles no return type signatures by overloading:

protected void SafeSocketCommand(Action socketCommand)
{
    SafeSocketCommand(() => { socketCommand(); return 0; });
}


If you modify your SafeSocketCommand to accept an Expression<Func<TResult>> then you'll get access to an expression tree that represents the body of the lambda, from which you can access the ReadCurrentBuffer call directly.

However, if you do this, you're no longer dealing with a regular anonymous method; to actually call it you'll need to compile the expression tree to code. You may also need to be flexible as to what your code expects to appear inside the lambda's body.


No, because lambda's don't have names; they're anonymous functions. You could get the method name from the last stackframe, though:

new StackFrame(1).GetMethod().Name;


Func<TResult> is just a delegate. Rather than use a lambda, create a method that matches the signature of Func<TResult> and call that. That way, you'll have whatever name you want.

SafeSocketCommand(MyNewMethod);

...

public TResult MyNewMethod()
{
    return this.mySocket.ReadCurrentBuffer();
}


In this case, you can simply this call instead. It'll be faster and smaller generated code too.

SafeSocketCommand(mySocket.ReadCurrentBuffer);

In general, the StackTrace of the Exception object contains the full information you are looking for, much more accurately than printing the method name, or you can use the TargetSite property for the name of the method that threw the exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消