C#获取方法的名称

[复制链接]
查看11 | 回复5 | 2016-10-14 00:38:14 | 显示全部楼层 |阅读模式
A方法调用了B函数中的方法:B_method(aa),在B中能否根据参数aa知道A的方法名 和所在的类名?

回复

使用道具 举报

千问 | 2016-10-14 00:38:14 | 显示全部楼层
用参数判断好像不行,不过你可以用StackTrace 捕捉到调用者
[STAThread]
static void Main(string[] args)
{
method2();
}
static private void method1()
{
StackTrace st = new StackTrace(true);
Console.WriteLine(st.GetFrame(1).GetMethod().Name.ToString());

回复

使用道具 举报

千问 | 2016-10-14 00:38:14 | 显示全部楼层
初学者吧,直接的方法是察看B的定义,可知aa的类型,当然也就知道它含啥方法。
回复

使用道具 举报

千问 | 2016-10-14 00:38:14 | 显示全部楼层
class Program
{
static void Main(string[] args)
{
new AA().AAMethod();
Console.Read();
}
public class AA

回复

使用道具 举报

千问 | 2016-10-14 00:38:14 | 显示全部楼层
应该是不可以
回复

使用道具 举报

千问 | 2016-10-14 00:38:14 | 显示全部楼层
不能
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行