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

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

转载于:https://www.cnblogs.com/nafio/p/9137411.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值