Solved

Use reflection to discover information at runtime

Posted on 2012-03-22
3
167 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now