C#获得当前执行的函数名、当前代码行、源代码文件名
http://blog.csdn.net/newegg2009/article/details/6220385
C#获得当前执行的函数名、当前代码行、源代码文件名
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
this.Text = st.GetFrame(0).ToString();
得到代码行,源代码文件名:
StackTrace st = new StackTrace(new StackFrame(true));
Console.WriteLine(" Stack trace for current level: {0}", st.ToString());
StackFrame sf = st.GetFrame(0);
Console.WriteLine(" File: {0}", sf.GetFileName());
Console.WriteLine(" Method: {0}", sf.GetMethod().Name);
Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());
Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());
private void btnException_Click(object sender, System.EventArgs e)
{
try
{
ProcException1(1, 2);
}
catch(Exception exp)
{
GetFullStackFrameInfo(new StackTrace(exp));
}
}
EventArgs e)
{
try
{
SaveSomething();
Alert.Show("You document has been saved");
}
catch (ReadOnlyException)
{
Alert.Show("You do not have write permission to this file");
}
}
private void btnStackTrace_Click(object sender, System.EventArgs e)
{
int x = 2;
ProcA(1, ref x, "Hello");
}
private void GetFullStackFrameInfo(StackTrace st)
{
int fc = st.FrameCount;
lstStackItems.Items.Clear();
for(int i = 0; i < fc - 1; i++)
{
lstStackItems.Items.Add(GetStackFrameInfo(st.GetFrame(i)));
}
}
private string GetStackFrameInfo(StackFrame sf)
{
string strParams;
MethodInfo mi;
Type typ;
string strOut = string.Empty;
mi = (MethodInfo) sf.GetMethod();
if (mi.IsPrivate)
{
strOut += "private ";
}
else if ( mi.IsPublic )
{
strOut += "public ";
}
else if ( mi.IsFamily )
{
strOut += "protected ";
}
else if ( mi.IsAssembly )
{
strOut += "internal ";
}
if ( mi.IsStatic )
{
strOut += "static ";
}
strOut += mi.Name + "(";
ParameterInfo[] piList = sf.GetMethod().GetParameters();
strParams = string.Empty;
foreach(ParameterInfo pi in piList)
{
strParams += string.Format(", {0} {1} {2}", ((pi.ParameterType.IsByRef) ? "ByRef" : "ByVal"), pi.Name, pi.ParameterType.Name);
}
if (strParams.Length > 2)
{
strOut += strParams.Substring(2);
}
typ = mi.ReturnType;
strOut += ") " + typ.ToString();
return strOut;
}
private void ProcException1(int x, int y)
{
ProcException2("Mike", 12);
}
private void ProcException2(string Name, long Size)
{
ProcException3();
}
private string ProcException3()
{
return ProcException4("mike@microsoft.com");
}
private string ProcException4(string EmailAddress)
{
throw new ArgumentException("This is a fake exception!");
}
private void ProcA(int Item1, ref int Item2, string Item3)
{
ProcB(string.Concat(Item1, Item2, Item3));
}
private void ProcB(string Name)
{
GetFullStackFrameInfo(new StackTrace());
}
{
{
dirinfo.Attributes = FileAttributes.Normal;
}
DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
{
case "FullControl":
dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
break;
case "ReadOnly":
dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));
break;
case "Write":
dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));
break;
case "Modify":
dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow));
break;
}
dirinfo.SetAccessControl(dirsecurity);
}
{
// Cleans the message to allow single quotation marks
string cleanMessage = message.Replace("'", "//'");
string script = "<script
type=/"text/javascript/">alert('" + cleanMessage + "');</script>";
// Gets the executing web page
Page page = HttpContext.Current.CurrentHandler as Page;
// Checks if the handler is a Page and that the script isn't allready on the Page
if (page != null &&
!page.ClientScript.IsClientScriptBlockRegistered("alert"))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert",
script);
}
}
}
C#获得当前执行的函数名、当前代码行、源代码文件名的更多相关文章
- Java获取正在执行的函数名
利用StackTrace堆栈轨迹获取某个时间的调用堆栈状态. package com.dsp.demo; public class TechDemo { public static void main ...
- 使用 Addr2line 将函数地址解析为函数名
用 Addr2line 将函数地址解析为函数名 原文链接:http://www.ibm.com/developerworks/cn/linux/l-graphvis/ Addr2line 工具(它是标 ...
- JS 中函数名后面加与不加括号的区别
a.onmouseover = fn1; a.onmouseout = fn2; function fn1(){ div.className = "erweima show"; } ...
- 跟着太白老师学python day11 函数名的应用 globals(), locals()
1. 函数名就是内存地址 def func(): ') print(func) >>>> <function func at 0x00000000003DC1E0> ...
- C++ DLL导出函数的两种方法(导出序号那种方法,别人看不到函数名)
第一种就直接导出函数名如下代码: #ifdef__cplusplus #define TEXPORT extern "c" _declspec(dllexport) #dlse # ...
- python 动态获取当前运行的类名和函数名的方法
一.使用内置方法和修饰器方法获取类名.函数名 python中获取函数名的情况分为内部.外部,从外部的情况好获取,使用指向函数的对象,然后用__name__属性 复制代码代码如下: def a():pa ...
- JS函数 函数调用 函数定义好后,是不能自动执行的,需要调用它,直接在需要的位置写函数名。
函数调用 函数定义好后,是不能自动执行的,需要调用它,直接在需要的位置写函数名. 第一种情况:在<script>标签内调用. <script type="text/java ...
- 八、React实战:可交互待办事务表(表单使用、数据的本地缓存local srtorage、生命同期函数(页面加载就会执行函数名固定为componentDidMount()))
一.项目功能概述 示例网址:http://www.todolist.cn/ 功能: 输入待做事项,回车,把任务添加到 [正在进行] [正在进行] 任务,勾选之后,变成已[经完成事项] [已完成事务], ...
- Javascript自执行匿名函数(function() { })()的原理分析
匿名函数指没有指定函数名或指针的函数,自执行匿名函数只是其中一种,下文中称这种函数为:自执行函数 下面是一个最常见的自执行函数: // 传统匿名函数 (function() { alert('hell ...
随机推荐
- hdu1028(母函数+DP)
题目信息:求分解整数n的个数q(n);能够母函数或者DP http://acm.hdu.edu.cn/showproblem.php?pid=1028 AC代码: /***************** ...
- Time To First Byte (TTFB) 第一字节时间 页面加载时间
Time to first byte - Wikipedia https://en.wikipedia.org/wiki/Time_to_first_byte Time to first byte ( ...
- 无法启动FTP站点,服务目前停止
在本地搭建了一个FTP服务器(windows搭建FTP服务器实战),再启动的时候提示错误: 错误提示信息: 根据提示可以查出问题原因:FTP服务没有开启 启动服务,再次重启站点服务.一切OK. 亲测好 ...
- 往android的内核添加驱动及 ueventd.rc 修改【转】
本文转载自:http://blog.csdn.net/lkqboy2599/article/details/8350100 向android的内核添加驱动,其实就是向linux内核添加驱动.主要在两个 ...
- LightOJ1138 —— 阶乘末尾0、质因子分解
题目链接:https://vjudge.net/problem/LightOJ-1138 1138 - Trailing Zeroes (III) PDF (English) Statistic ...
- html5--4-2 video元素的属性
html5--4-2 video元素的属性 学习要点 掌握video元素的基本用法 直到现在,在网页中的大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件.HTML ...
- linux应用之openssh server安装及配置(centos)
安装OpenSSH Server 首先,我们搜索一下CentOS的软件库里面有没有已经定义好的SSH服务器包: $ yum search ssh ... ... openssh.x86_64 : An ...
- hdu-5000 Clone(dp)
题目链接: Clone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pro ...
- Python: PS 滤镜--USM 锐化
本文用 Python 实现 PS 滤镜中的 USM 锐化效果,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/detail ...
- object_test.py
#方法,属性,私有化加双下划线 ''' __a 从外部无法访问,但是类的内部可以访问.实际上还是能在类外访问这些私有方法,尽管不应该这么做:s._A__a 如果不需要使用这种方法但是又不行让其他对象不 ...