Solved

Use reflection to discover information at runtime

Posted on 2012-03-22
3
170 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 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:Tom Knowlton
ID: 37757604
Thank you, both.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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