转:.NET获取当前方法名或调用此方法的方法名
Introduction
Before .NET, we were always looking for a way to log current method name in a log file for better logging. But, there were no functionalities that could have helped in this, and it was left as an uncompleted job.
But, with .NET, we could easily find out the name of the current method or parent method. This has been accomplished by StackFrame, StackTrace, and MethodBase classes in System.Diagnostics and System.Reflection namespaces.
StackFrameprovides information about function call on stack call for current process.StackTraceis a collection ofStackFrame.MethodBaseis an abstract class containing information about current method.
Note: When an exception occurs in the method, exception object contains a reference to the StackTrace object that could be used to log the method name. But for logging a method name without an error generated, we need to read it from the stack, using StackFrame class.
In the sample, MethodBase object would reference to current function on stack call returned by StackFrame object. To get the parent method, we would use StackTrace to get parent’s StackFrame object.
Create a new console application:
Add namespaces:
using System.Diagnostics;
using System.Reflection; [STAThread]
static void Main(string[] args)
{
WhatsMyName();
}
// function to display its name
private static void WhatsMyName()
{
StackFrame stackFrame = new StackFrame();
MethodBase methodBase = stackFrame.GetMethod();
Console.WriteLine(methodBase.Name ); // Displays “WhatsmyName”
WhoCalledMe();
}
// Function to display parent function
private static void WhoCalledMe()
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame();
MethodBase methodBase = stackFrame.GetMethod();
// Displays “WhatsmyName”
Console.WriteLine( " Parent Method Name {0} ", methodBase.Name );
}
Note: This feature is not available in .NET Compact Framework as StackFrame class is unavailable. For that, you would need to use same old method of manually passing method name to the logging function.
http://www.codeproject.com/Articles/7964/Logging-method-name-in-NET
http://www.cnblogs.com/ndaysy/p/3297768.html
常用方式:
StackFrame sf = new StackFrame();
log.Error(sf.GetMethod().DeclareTypeName);
转:.NET获取当前方法名或调用此方法的方法名的更多相关文章
- java根据方法名动态调用invoke方法!
public class Activity { public void deal(String name, long id) { System.out.println(name + id + &quo ...
- Android获取APK包名的几种方法
Android获取APK包名的几种方法:1.adb shell pm list package -f | findstr 关键字 #只能获取到包名,主Activity名无法获取到 2.使用aapt-- ...
- PHP获取文件后缀名的三种方法
如下: <? PHP获取文件后缀名的几种方法1: function get_file_type($filename){ $type = substr($filename, strrpos($fi ...
- 使用js方法时,调用的方法名明明一致,但就是不管用,解决
前提:代码全部写对 问题:调用的方法名明明一致,但就是不管用 举例:写了个function delete(){}方法, 点击调用delete方法,onclik="delete()" ...
- PHP中获取文件扩展名的N种方法
PHP中获取文件扩展名的N种方法 从网上收罗的,基本上就以下这几种方式: 第1种方法:function get_extension($file){substr(strrchr($file, '.'), ...
- iOS JS 交互之利用系统JSContext实现 JS调用OC方法以及Objective-C调用JavaScript方法
ios js 交互分为两块: 1.oc调用js 这一块实现起来比较简单, 我的项目中加载的是本地的html,js,css,需要注意的是当你向工程中拖入这些文件时,选择拷贝到工程中,(拖入的文件夹是蓝色 ...
- C# 解析js方法,并调用js方法
本文转载:http://www.cnblogs.com/StudyLife/archive/2013/03/11/2953516.html 本文不是基于B/S的 后台调用前台js方法,而是给你一段js ...
- 调用start()方法和直接调用run()方法的区别
调用start()方法和直接调用run()方法的区别 新建一个线程,只需要使用new关键字创建一个线程对象,并且调用start()方法即可. Thread thread = new Thread(); ...
- 134-PHP子类重写父类方法,并调用父类方法
<?php class father{ //定义father类 public function method(){ //定义方法 echo '<br />father method' ...
随机推荐
- [BZOJ1659][Usaco2006 Mar]Lights Out 关灯
[BZOJ1659][Usaco2006 Mar]Lights Out 关灯 试题描述 奶牛们喜欢在黑暗中睡觉.每天晚上,他们的牲口棚有L(3<=L<=50)盏灯,他们想让亮着的灯尽可能的 ...
- delphi 枚举类型
枚举类型定义了一系列有序值的集合.枚举变量就是从这个既定的集合中取某个值.集合中的有序值可以称为元素,元素一般从0开始索引(也就是元素的顺序号). 定义一个枚举类型,采用以下的格式: type typ ...
- Objective-C中的instancetype和id区别
目录(?)[-] 有一个相同两个不同相同 Written by Mattt Thompson on Dec 10th 2012 一什么是instancetype 二关联返回类型related resu ...
- android文章学习 侧滑菜单实现
http://blog.csdn.net/jj120522/article/details/8075249 http://blog.csdn.net/lilybaobei/article/detail ...
- ubuntu安装到选择位置时闪退
转自:http://tieba.baidu.com/p/3020839207
- java中四种阶乘的计算
package com.zf.s2;//创建一个包 import java.math.BigInteger;//导入类 import java.util.ArrayList; import jav ...
- Linux匿名管道与命名管道
http://blog.chinaunix.net/uid-26000296-id-3408970.html /* * \File * main.c * \Descript * father-proc ...
- VIM替换、截取及追加操作
参考: http://blog.csdn.net/love__coder/article/details/6739670 http://blog.csdn.net/love__coder/articl ...
- 15.含有指针成员的类的拷贝[ClassCopyConstructorWithPointerMember]
[题目] 下面是一个数组类的声明与实现.请分析这个类有什么问题,并针对存在的问题提出几种解决方案. C++ Code 123456789101112131415161718192021222324 ...
- Android runProguard配置 导致module lib 中的包编译时无法识别
今天写代码时用到了另一个lib型的工程,把它添加到dependencies后,在原工程中可以引用lib中的文件了,但是编译时就会报错,提示包不存在,后来在build.gradle中设置runProgu ...