Reputation: 690
I wrote a class and I want to return the names of function which I implemented inside the class.
public class cls
{
public static string fun()
{
//code
}
}
Simply I want to know how to know the name of the function (which is fun
), not the return value of that function.
Upvotes: 2
Views: 228
Reputation: 5194
public static void TraceContext(string messageFormat)
{
Trace.WriteLine(string.Format(messageFormat,
new System.Diagnostics.StackFrame(1).GetMethod().Name));
}
See How To: Obtain Method Name Programmatically For Tracing
Upvotes: 4
Reputation: 5747
For the case in your question, this will work:
var methods = typeof (Messages).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);
foreach(var method in methods)
{
Console.WriteLine(method.Name);
}
If you need other data (for example, inherited methods from a parent class), you'll need other combinations of BindingFlags.
Upvotes: 1
Reputation: 1271
Um, what? The Class name is Messages. The Function is named Saved. Either you've made a typo, or you've omitted something important from your code/description.
In any case, the functionality you're after is contained in the Reflection namespace.
No offense intended, but based on how you've worded your description, I'm going to assume no prior knowledge of reflection, in which case I recommend the following introductory tutorial
Upvotes: 0
Reputation: 3806
You can use reflection to do this
typeof(Messages).GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Select(m=>m.Name)
.ToArray()
Upvotes: 1
Reputation: 1535
You need to use reflection, try http://www.csharp-examples.net/get-method-names/
Upvotes: 3