谁叫我方法??? [英] Who called me method???

查看:43
本文介绍了谁叫我方法???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道是谁调用了某个对象的某个方法(从中调用了它的b $ b类/方法)


EG

class A

{

Public A()

{

B b = new B( );

}

私人一些()

{

b.Caller();

}

}

B级

{

公共来电者()

{

Console.WriteLine(" Called from {0}",WHO_CALL_ME);

}

}


输出:从A.Some()调用

How can I know who called the certain method of some object (from which
class/method it was called)

E.G.
class A
{
Public A()
{
B b = new B();
}
Private Some()
{
b.Caller();
}
}
Class B
{
Public Caller()
{
Console.WriteLine ("Called from {0}",WHO_CALL_ME);
}
}

Output: "Called from A.Some()"

推荐答案

Tamir,


为了做到这一点,你必须实例化

StackFrame类的新实例。你需要将1传递给构造函数,让它知道

在堆栈中向上移动一帧。一旦你有了,你可以调用

GetMethod方法来获得表示调用你的

方法的MethodBase实现。最后,使用Name属性获取名称。


希望这会有所帮助。

-

- Nicholas Paldino [。 NET / C#MVP]

- mv*@spam.guard.caspershouse.com

Tamir Khason < TA ********** @ tcon-NOSPAM.co.il>在消息中写道

新闻:eW ************** @ TK2MSFTNGP09.phx.gbl ...
Tamir,

In order to do this, you will have to instantiate a new instance of the
StackFrame class. You need to pass 1 to the constructor to let it know to
go up one frame in the stack. Once you have that, you can call the
GetMethod method to get the MethodBase implementation representing the
method that called you. Finally, use the Name property to get the name.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
我怎么知道谁称为某个对象的某种方法(从中调用它的类/方法)

EG
A类
{公开A()
{
B b = new B();
}
私人有些()
{
b.Caller();
} < B类
{
公共来电者()
{
Console.WriteLine(来自{0},WHO_CALL_ME); <输出:从A.Some()调用
How can I know who called the certain method of some object (from which
class/method it was called)

E.G.
class A
{
Public A()
{
B b = new B();
}
Private Some()
{
b.Caller();
}
}
Class B
{
Public Caller()
{
Console.WriteLine ("Called from {0}",WHO_CALL_ME);
}
}

Output: "Called from A.Some()"



Tamir,


为了做到这一点,你必须实例化

StackFrame类的新实例。你需要将1传递给构造函数,让它知道

在堆栈中向上移动一帧。一旦你有了,你可以调用

GetMethod方法来获得表示调用你的

方法的MethodBase实现。最后,使用Name属性获取名称。


希望这会有所帮助。

-

- Nicholas Paldino [。 NET / C#MVP]

- mv*@spam.guard.caspershouse.com

Tamir Khason < TA ********** @ tcon-NOSPAM.co.il>在消息中写道

新闻:eW ************** @ TK2MSFTNGP09.phx.gbl ...
Tamir,

In order to do this, you will have to instantiate a new instance of the
StackFrame class. You need to pass 1 to the constructor to let it know to
go up one frame in the stack. Once you have that, you can call the
GetMethod method to get the MethodBase implementation representing the
method that called you. Finally, use the Name property to get the name.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
我怎么知道谁称为某个对象的某种方法(从中调用它的类/方法)

EG
A类
{公开A()
{
B b = new B();
}
私人有些()
{
b.Caller();
} < B类
{
公共来电者()
{
Console.WriteLine(来自{0},WHO_CALL_ME); <输出:从A.Some()调用
How can I know who called the certain method of some object (from which
class/method it was called)

E.G.
class A
{
Public A()
{
B b = new B();
}
Private Some()
{
b.Caller();
}
}
Class B
{
Public Caller()
{
Console.WriteLine ("Called from {0}",WHO_CALL_ME);
}
}

Output: "Called from A.Some()"



太好了,谢谢。唯一的小事就是离开。如何获得名为

的方法?


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:Od ************** @ TK2MSFTNGP09.phx.gbl ...
Great, thank you. The only one small thing leave. How to get the class of
method called?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Od**************@TK2MSFTNGP09.phx.gbl...
Tamir,
为了做到这一点,你必须在StackFrame类中实例化一个
的新实例。你需要将1传递给构造函数,让它知道
在堆栈中向上移动一帧。完成后,您可以调用
GetMethod方法来获取表示调用您的
方法的MethodBase实现。最后,使用Name属性来获取名称。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP] - mv*@spam.guard.caspershouse.com

Tamir Khason < TA ********** @ tcon-NOSPAM.co.il>在消息中写道
新闻:eW ************** @ TK2MSFTNGP09.phx.gbl ...
Tamir,

In order to do this, you will have to instantiate a new instance of the StackFrame class. You need to pass 1 to the constructor to let it know to
go up one frame in the stack. Once you have that, you can call the
GetMethod method to get the MethodBase implementation representing the
method that called you. Finally, use the Name property to get the name.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
我怎么知道谁调用了某种方法一些对象(从中调用它的类/方法)

EG
A类
{
公共A()
{
B b = new B();
}
私人一些()
{
b.Caller();
}
}
B类
{
公共来电者()
{
Console.WriteLine(来自{0},WHO_CALL_ME);
}
}

输出:从A.Some()调用
How can I know who called the certain method of some object (from which
class/method it was called)

E.G.
class A
{
Public A()
{
B b = new B();
}
Private Some()
{
b.Caller();
}
}
Class B
{
Public Caller()
{
Console.WriteLine ("Called from {0}",WHO_CALL_ME);
}
}

Output: "Called from A.Some()"




这篇关于谁叫我方法???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆