简体   繁体   English

我怎么知道哪种方法调用我的方法?

[英]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引入一个新参数,让AB告诉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.

相关问题 我怎么知道哪个方法称为事件处理程序 - How can I know which method called event handler 如何知道哪个线程正在运行我的方法? - How to know which thread is running my method? 我如何才能调用此方法.. +检查我的方法是否正确 - How can i call this method .. + check if my method is correct or not api控制器如何知道不带注释的调用方法 - How does the api controller know which method to call without the annotation 为LINQ to Objects定义自己的Where-Method - 我怎么知道哪一个会被使用? - Defining my own Where-Method for LINQ to Objects - How do I know which one will get used? 初始化Web API时如何调用方法? - How can I call a method when my web API is initialized? 如何从绑定中调用方法并在iOS中重写? - How can I call a method from my bindings and override in iOS? 我需要在C#程序的VB文件中使用一种方法,该如何调用该方法? - I need to use a method in a VB file in my C# program, how can I call this method? 在调用扩展方法之后,如何让resharper知道我的变量不为null? - How can I get resharper to know that my variable is not null after I have called an extension method on it? 如何在对异步方法的调用上追加对扩展方法的调用 - How can I append a call to an extension method on a call to an async method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM