http://blog.csdn.net/newegg2009/article/details/6220385

C#获得当前执行的函数名、当前代码行、源代码文件名

[日期:2010-10-18 11:40]   来源:.NET中国学习网  作者:admin   [字体:  ]
  得到函数名:

  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());
using System.Diagnostics;
http://madskristensen.net/post/JavaScript-AlertShow(e2809dmessagee2809d)-from-ASPNET-code-behind.aspx

private void btnException_Click(object sender, System.EventArgs e)

{

try 

{

ProcException1(1, 2);

}

catch(Exception exp)

{

GetFullStackFrameInfo(new StackTrace(exp));

}

}
void btnSave_Click(object sender,
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());

}
        private static void addpathPower(string pathname, string username, string power)

        {
            DirectoryInfo dirinfo = new DirectoryInfo(pathname);
            if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)

            {

                dirinfo.Attributes = FileAttributes.Normal;

            }
            //取得访问控制列表   

            DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
            switch (power)

            {

                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);

        }
public static void Show(string message)

{
   // 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#获得当前执行的函数名、当前代码行、源代码文件名的更多相关文章

  1. Java获取正在执行的函数名

    利用StackTrace堆栈轨迹获取某个时间的调用堆栈状态. package com.dsp.demo; public class TechDemo { public static void main ...

  2. 使用 Addr2line 将函数地址解析为函数名

    用 Addr2line 将函数地址解析为函数名 原文链接:http://www.ibm.com/developerworks/cn/linux/l-graphvis/ Addr2line 工具(它是标 ...

  3. JS 中函数名后面加与不加括号的区别

    a.onmouseover = fn1; a.onmouseout = fn2; function fn1(){ div.className = "erweima show"; } ...

  4. 跟着太白老师学python day11 函数名的应用 globals(), locals()

    1.  函数名就是内存地址 def func(): ') print(func) >>>> <function func at 0x00000000003DC1E0> ...

  5. C++ DLL导出函数的两种方法(导出序号那种方法,别人看不到函数名)

    第一种就直接导出函数名如下代码: #ifdef__cplusplus #define TEXPORT extern "c" _declspec(dllexport) #dlse # ...

  6. python 动态获取当前运行的类名和函数名的方法

    一.使用内置方法和修饰器方法获取类名.函数名 python中获取函数名的情况分为内部.外部,从外部的情况好获取,使用指向函数的对象,然后用__name__属性 复制代码代码如下: def a():pa ...

  7. JS函数 函数调用 函数定义好后,是不能自动执行的,需要调用它,直接在需要的位置写函数名。

    函数调用 函数定义好后,是不能自动执行的,需要调用它,直接在需要的位置写函数名. 第一种情况:在<script>标签内调用. <script type="text/java ...

  8. 八、React实战:可交互待办事务表(表单使用、数据的本地缓存local srtorage、生命同期函数(页面加载就会执行函数名固定为componentDidMount()))

    一.项目功能概述 示例网址:http://www.todolist.cn/ 功能: 输入待做事项,回车,把任务添加到 [正在进行] [正在进行] 任务,勾选之后,变成已[经完成事项] [已完成事务], ...

  9. Javascript自执行匿名函数(function() { })()的原理分析

    匿名函数指没有指定函数名或指针的函数,自执行匿名函数只是其中一种,下文中称这种函数为:自执行函数 下面是一个最常见的自执行函数: // 传统匿名函数 (function() { alert('hell ...

随机推荐

  1. 在VS2015中增加JQuery引用及智能提示

      打开VS2015,从"工具"菜单选择NuGet选项,搜索Jquery,并点击安装.   可以看到解决方案的scripts增加了对应文件的引用   在HTML文件中可以直接引用j ...

  2. 51 NOD 1753 相似子串 字符串hash

      1735  相似子串  基准时间限制:5 秒 空间限制:131072 KB 分值: 80   两个字符串相似定义为:1.两个字符串长度相等2.两个字符串对应位置上有且仅有至多一个位置所对应的字符不 ...

  3. EasyVideoRecorder短视频拍摄、短视频录制SDK支持IOS版本

    在前面的博客<EasyDarwin开发出类似于美拍.秒拍的短视频拍摄SDK:EasyVideoRecorder>和<美拍.秒拍中安卓.IOS短视频拍摄的一些关键技术>中我们简单 ...

  4. Android笔记之强大的buildConfigField

    在进行项目开发或维护时,经常会遇到调试和发布所用到的参数值不一致的情况 例如,服务器会分测试和正式,为了能方便地更改(自动更换)服务器地址,buildConfigField就派上用场了 以前都是手动更 ...

  5. python基础教程_学习笔记18:标准库:一些最爱——shelve

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/36029981 标准库:一些最爱 shelve S ...

  6. 分享一个utils.js源码

    NEJ.define([ './global.js', '{platform}util.js' ],function(NEJ,_h,_p,_o,_f,_r){ /* * 查看数据是否指定类型 * @p ...

  7. spl处理文件(文件详细信息、文件遍历、查询指定行、写入CSV文件)

    <?php /** * 文件操作 */ //常用操作 $file = new SplFileInfo('D:/workspace/xlyy/spl/test.txt'); $file_info ...

  8. (linux)struct inode 和 struct file

    转自:http://www.cnblogs.com/QJohnson/archive/2011/06/24/2089414.html 1.struct inode──字符设备驱动相关的重要结构介绍 内 ...

  9. 安装MySQLdb出现的问题

    枫竹梦的环境是自己编译安装的MySQL,安装目录在/usr/local/mysql. 下载MySQLdb,由于网络上大多数的链接都是指向比较老的sourceforge上,而我们安装最新的1.2.5,h ...

  10. 基于BASYS2的VHDL程序——数字钟(最终版)

    转载请注明原地址:http://www.cnblogs.com/connorzx/p/3674178.html 调时电路正常工作.一切正常.发现做FPGA还是得从数电的思路思考,设置一个预置使能端,预 ...