Solved

Use reflection to discover information at runtime

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

12 Experts available now in Live!

Get 1:1 Help Now