Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Use reflection to discover information at runtime

Posted on 2012-03-22
3
Medium Priority
?
177 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 24

Assisted Solution

by:alexey_gusev
alexey_gusev earned 400 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 1600 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:Tom Knowlton
ID: 37757604
Thank you, both.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 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