Who is online? 165 guests and 0 members
Member login | Become a member
Posted: 3/7/2010 12:49:36 PM
Hi experts,
I have a requirement where I need to print all the methods inside a class. Can you help me how can I get the list of methods ?
Posted: 3/9/2010 8:49:18 PM
Hi,
You can make use of System.Reflection. Try this sample I am showing you an example to get list of methods of "string" class
using System; using System.Reflection; public class Example { public static void Main() { Type type = typeof(string); MethodInfo[] methods = type.GetMethods(); foreach (MethodInfo method in methods) { Console.WriteLine(method.Name); } Console.ReadLine(); } }
Output:
JoinJoinEqualsEqualsEqualsEqualsEqualsop_Equalityop_Inequalityget_CharsCopyToToCharArrayToCharArrayIsNullOrEmptyGetHashCodeget_LengthSplitSplitSplitSplitSplitSplitSubstringSubstringTrimTrimStartTrimEndIsNormalizedIsNormalizedNormalizeNormalizeCompareCompareCompareCompareCompareCompareCompareCompareCompareCompareCompareToCompareToCompareOrdinalCompareOrdinalContainsEndsWithEndsWithEndsWithIndexOfIndexOfIndexOfIndexOfAnyIndexOfAnyIndexOfAnyIndexOfIndexOfIndexOfIndexOfIndexOfIndexOfLastIndexOfLastIndexOfLastIndexOfLastIndexOfAnyLastIndexOfAnyLastIndexOfAnyLastIndexOfLastIndexOfLastIndexOfLastIndexOfLastIndexOfLastIndexOfPadLeftPadLeftPadRightPadRightStartsWithStartsWithStartsWithToLowerToLowerToLowerInvariantToUpperToUpperToUpperInvariantToStringToStringCloneTrimInsertReplaceReplaceRemoveRemoveFormatFormatFormatFormatFormatCopyConcatConcatConcatConcatConcatConcatConcatConcatConcatInternIsInternedGetTypeCodeGetEnumeratorGetType
Posted: 3/9/2010 9:07:37 PM
If you need distinct names:
using System; using System.Linq; using System.Reflection; public class Example { public static void Main() { Type type = typeof(string); MethodInfo[] methods = type.GetMethods(); foreach (var name in (from m in methods select m.Name).Distinct()) { Console.WriteLine(name); } Console.ReadLine(); } }
JoinEqualsop_Equalityop_Inequalityget_CharsCopyToToCharArrayIsNullOrEmptyGetHashCodeget_LengthSplitSubstringTrimTrimStartTrimEndIsNormalizedNormalizeCompareCompareToCompareOrdinalContainsEndsWithIndexOfIndexOfAnyLastIndexOfLastIndexOfAnyPadLeftPadRightStartsWithToLowerToLowerInvariantToUpperToUpperInvariantToStringCloneInsertReplaceRemoveFormatCopyConcatInternIsInternedGetTypeCodeGetEnumeratorGetType
Also if you need to sort it by name:
using System; using System.Linq; using System.Reflection; public class Example { public static void Main() { Type type = typeof(string); MethodInfo[] methods = type.GetMethods(); foreach (var name in (methods.OrderBy(m => m.Name).Select(m => m.Name)).Distinct()) { Console.WriteLine(name); } Console.ReadLine(); } }
CloneCompareCompareOrdinalCompareToConcatContainsCopyCopyToEndsWithEqualsFormatget_Charsget_LengthGetEnumeratorGetHashCodeGetTypeGetTypeCodeIndexOfIndexOfAnyInsertInternIsInternedIsNormalizedIsNullOrEmptyJoinLastIndexOfLastIndexOfAnyNormalizeop_Equalityop_InequalityPadLeftPadRightRemoveReplaceSplitStartsWithSubstringToCharArrayToLowerToLowerInvariantToStringToUpperToUpperInvariantTrimTrimEndTrimStart