开发者

i want to make a function or macro that will return the current code location in the format of namespace.class.function()

开发者 https://www.devze.com 2022-12-11 19:01 出处:网络
i basically want to call this function code to get some metrics and so it needs to know what is the locati开发者_运维问答on of the execution code right now.

i basically want to call this function code to get some metrics and so it needs to know what is the locati开发者_运维问答on of the execution code right now. so i can use this location to log the code executing position. is this possible?


Have a look at System.Diagnostics.StackTrace.

public void CallerTest()
{
    CallerInformation();
}

public void CallerInformation()
{
    System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
    System.Diagnostics.StackFrame caller = stackTrace.GetFrame(1); // The caller
    int callLineNumber = caller.GetFileLineNumber();
    System.Reflection.MethodBase callerMethod = caller.GetMethod();
    string callerName = callerMethod.ReflectedType.FullName + "." + callerMethod.Name;
    System.Diagnostics.Debug.Write(string.Format("Caller - Line:{0} Method:{1}", callLineNumber, callerName));
}


The short answer is - yes, I guess it's possible, but I'm not sure. You'll need to read up on Reflection - you can dynamically construct and compile assemblies on the fly, so I would imagine you'll be able to do what you want - or something near enough.

Sorry - the way you've worded your question makes it a little hard to understand what you want to do. Ignoring the technical specifics of what you want to do (inject a function) - what is it you're actually trying to achieve?

0

精彩评论

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