Solved

Use reflection to discover information at runtime

Posted on 2012-03-22
3
168 Views
Last Modified: 2012-03-23
Is it possible to do the following?

public SomeClass
{
private DiscoverClass discObj = new DiscoverClass( );

public void SomeMethod(string someparam1, int someparam2)
{
    int somelocalvar;
    SomeClass someobj = new SomeClass( );

    discObj.DoDiscovery();


}
}




public class DiscoveryClass
{
     public DiscoveryClass(){}

    public void DoDiscovery(  )
    {
         System.Diagnostics.Debug.WriteLine("Entered method <<SomeMethod>> which is inside class <<SomeClass>>");
    }
}

Open in new window




 <<SomeMethod>>
and
<<SomeClass>>


would be found using Reflection.



Possible?
0
Comment
Question by:knowlton
3 Comments
 
LVL 24

Assisted Solution

by:alexey_gusev
alexey_gusev earned 100 total points
ID: 37756275
I'm not quite following what you want but something like http://msdn.microsoft.com/en-us/library/system.reflection.typedelegator.aspx would help
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 400 total points
ID: 37756774
Tom,

If you are just trying to get the information about the method, there is the System.Diagnostics.StackFrame class that should do the trick.

The joys of System.Diagnostics.StackFrame
http://mikehadlow.blogspot.com/2007/09/joys-of-systemdiagnosticsstackframe.html

StackFrame stackFrame = new StackFrame(1, false);
MethodBase method = stackFrame.GetMethod();
Console.WriteLine("The name of the method was: {0}", method.Name);

Open in new window


The constructor for the StackFrame has different overloads.  The example is using this constructor:

StackFrame Constructor (Int32, Boolean)
http://msdn.microsoft.com/en-us/library/deysbxxw.aspx

skipFrames
Type: System.Int32
The number of frames up the stack to skip.

fNeedFileInfo
Type: System.Boolean
true to capture the file name, line number, and column number of the stack frame; otherwise, false.

The stack is in reverse order, so 0 = current method, 1 = calling method, etc.  This is how you get the stack trace when exceptions occur.
0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 37757604
Thank you, both.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question