Question

while writing the question subject I came across some other allmost related post ...leading to MSDN http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo.aspx

but i couldn't manage to extract the bit of code i needed

i just learnd how to get method name with a helper method based on st and sf as following :

    public void setLogView(View ViewMode)
    {
        AAdToAppLog();

        Lview_AH_AutomationLog.Sorting = SortOrder.Ascending;
        ColumnHeader ColHeadRowNo = new ColumnHeader();
        ColumnHeader ColHeadFunction = new ColumnHeader();
        ColumnHeader ColHeadContent = new ColumnHeader();
        ColumnHeader ColHeadTime = new ColumnHeader();

        Lview_AH_AutomationLog.View = ViewMode;
        Lview_AH_AutomationLog.Columns.Add(ColHeadRowNo);
        Lview_AH_AutomationLog.Columns.Add(ColHeadFunction);
        Lview_AH_AutomationLog.Columns.Add(ColHeadContent);
        Lview_AH_AutomationLog.Columns.Add(ColHeadTime);
        ColHeadRowNo.Text = "#";
        ColHeadFunction.Text = "Function Name";
        ColHeadContent.Text = "Content";
        ColHeadTime.Text = "Time";
        ColHeadRowNo.Width = 45;
        ColHeadFunction.Width = 150;
        ColHeadContent.Width = 150;
        ColHeadTime.Width = 100;
    }



    public void AAdToAppLog(string FunctionOutPut = "N/A")
    {
        string t = DateTime.Now.ToString("mm:ss.ff");
        string h = DateTime.Now.ToString("HH");
        ListViewItem FirstCell= new ListViewItem();
        FirstCell.Text =h+":" +pre0Tosingle_9(LogCounter.ToString());//Another helper puts 0 infront <=9 digits
        Lview_AH_AutomationLog.Items.Insert(0, FirstCell);
        StackTrace st = new StackTrace(); 
        StackFrame sf = st.GetFrame(1);
        string FunctionName = sf.GetMethod().ToString().Replace("Void", "").Replace("System.Windows.Forms.", ""); 
        FirstCell.SubItems.Add(FunctionName);
        FirstCell.SubItems.Add(FunctionOutPut);
        FirstCell.SubItems.Add(t);

        LogCounter++;

    }

so in every method i want i just put the

AddToAppLog()

that method contains my call to AddToAppLog() and then reports(Via ListView) the name of method and i just added the time of the call.

there's two things i would like to address in this post , about my implementation of that helper metod : the "FunctionName" i recive from sf.GetMethod is nice that it throws the type of the Parameter of a given Method i liked the idea , only that it is containing parameter.type's Father + Grandfather + Great-Grandfather, but i would like only the bottom Type :

System.Windows.Forms.View

this is one of the shortest (: and i tried to get to View by it self via playing with .Replace() so is there anothere way to strip it down or actually another method in same family that extract it the way i mentiond above or i could use a list containing every possible most-used types and do a foreach with stringReplace?

and more importently , how do i get the Method(parameterName) as well ?

thanks alot in advance !!

can someone Show An easy to comprehend , Simple syntax Example of getting parameters name ?

Was it helpful?

Solution

The crux of this is the line that involves sf.GetMethod(); if you instead store that:

var method = sf.GetMethod();
string name = method.Name;
var parameters = method.GetParameters();

you have access to the full signature, and note that the .Name is just the simple name - not the full declaring type-name, etc. You can of course access method.DeclaringType.Name if you want more context. Note that you can only get the declaration of the parameters; you can't get their values via any normal mechanism.

However!!! I will also observe that all this has a performance cost associated with reflection and stack-walking. If you have the C# 5 compiler, you may prefer:

public void AAdToAppLog([CallerMemberName] string callerName = "")
{ ... }

which means that the compiler will supply the name of the caller voluntarily (as a constant geneated in the IL) - no need to either supply it, or go walking the stack to figure it out. You cannot, however, get the parameters like this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top