[英]How can I know which method call my method?
I've 3 methods A(), B() and C(), both A() and B() call C(). 我有3个方法A(),B()和C(),A()和B()都调用C()。 In method C(), how can I know it call from A() or B()?
在方法C()中,如何从A()或B()调用它?
You shouldn't need to. 你不应该这样做。 Some method should perform a specific task, which is influenced by its parameters and the object attributes, not the caller.
某些方法应该执行特定任务,该任务受其参数和对象属性的影响,而不是调用者。
I don't recommend this approach - other posters point out better ways of handling this. 我不推荐这种方法 - 其他海报指出了处理这种方法的更好方法。 But if you really, really need to know who called you, without changing
C()
's parameters, you can do this: 但如果你真的需要知道谁给你打电话,而不改变
C()
的参数,你可以这样做:
static void A()
{
C();
}
static void C()
{
StackTrace st = new StackTrace();
Console.WriteLine(st.GetFrame(1).GetMethod().Name); // prints "A"
}
Note that generating a StackTrace is somewhat costly. 请注意,生成StackTrace有点昂贵。 Not a big deal, though, unless you're doing it in code that you're calling very frequently.
但是,这并不是什么大问题,除非你在代码中这么做,而且你经常打电话。
Again, you almost certainly find a better way of doing whatever you're trying to do. 同样,你几乎肯定会找到一种更好的方式来做你想做的事情。
Method C() should not need to know which method called it. 方法C() 不需要知道调用它的方法。 If this is how you are handling your flow logic, you need to think again about how you are writing your code.
如果这是您处理流逻辑的方式,则需要再次考虑如何编写代码。 If we assume that there is some valid reason for needing to know which method called C(), I would create two 'wrapper' methods: C_From_A() and C_From_B().
如果我们假设有一些有效的理由需要知道哪个方法叫做C(),我会创建两个'包装器'方法:C_From_A()和C_From_B()。 Any logic specific to the calling methods should be moved to the new methods, while common code is left in the C() method and called from both the new methods:
任何特定于调用方法的逻辑都应该移动到新方法,而公共代码保留在C()方法中并从两个新方法调用:
public void C()
{
// Common Code goes here
}
public void C_From_A()
{
// Code only to be called from A() goes here.
C(); // Common code executed
}
public void C_From_B()
{
// Code only to be called from B() goes here.
C(); // Common code executed
}
public void A()
{
// Other code goes here
C_From_A();
}
If you need to know for debugging, simple use the debugger to step through your code. 如果您需要了解调试,只需使用调试器来逐步执行代码。
You should to take a look into System.Diagnostics.StackFrame
class. 您应该查看
System.Diagnostics.StackFrame
类。 An example here: http://www.csharp-examples.net/reflection-callstack/ 这里有一个例子: http : //www.csharp-examples.net/reflection-callstack/
简单(干净)的方法是向C
引入一个新参数,让A
和B
告诉C
谁调用它。
只需在C()中设置断点
I agree in principle you shouldn't need to know in most situations. 我原则上同意你在大多数情况下都不需要知道。
However the one use case where this may be useful to know is when debugging where certain information came from, In the case of a bad parameter passed. 然而,这可能有用的一个用例是在调试某些信息来自哪里时,如果传递了错误的参数。
However in that case its probably better to throw an exception, log the exception and "recover" from it. 但是在这种情况下,最好抛出异常,记录异常并从中“恢复”。 Obviously this depends on how often the method is called as there is always some overhead with creating an exception.
显然,这取决于调用方法的频率,因为创建异常总会有一些开销。 If you need to do this for some other reason than I would suggest you look at your design first.
如果你出于某种其他原因需要这样做,我建议你先看看你的设计。
If you need callbacks I would suggest that you make A and B both implement an interface and pass A or B as a parameter. 如果你需要回调,我建议你让A和B都实现一个接口并传递A或B作为参数。 The interface could have a method called callback and C could call A or B.
接口可以有一个叫做回调的方法,C可以调用A或B.
在你试图找出“错误参数被传递”的位置的情况下,你只需要在该方法中设置一个条件断点,或者让VS中断抛出的异常,然后你可以检查调用堆栈( Debug Menu,Window,Call Stack)查看整个调用者链(带参数传递)给这个方法。
MethodBase callerMethod = new System.Diagnostics.StackFrame(1).GetMethod();
Useful if you're writing an audit / log framework, but really, YDNTN applies here. 如果您正在编写审计/日志框架,这很有用,但实际上,YDNTN适用于此处。 Plus it's costing a fortune at runtime.
而且它在运行时耗费了大量资金。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.