Click here to Skip to main content
15,124,449 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
class MyTestClass
   {
       static public void PrintMethods(string className)
       {

           try
           {
               Type classType = Type.GetType(className);

               var methodNames =
                   from methodInfo in classType.GetMethods()
                   where methodInfo.GetParameters().Any(parameterInfo => parameterInfo.ParameterType == typeof(string))
                   select methodInfo.Name;

               foreach (var method in methodNames)
                   Console.WriteLine(method);

           }
           catch (Exception)
           {

           }

       }

       static public void CallStaticMethod(string className, string methodName)
       {
           try
           {
               using (StreamReader reader = new StreamReader(@"data.txt"))
               {
                   Type.GetType(className).GetMethod(methodName, BindingFlags.Static | BindingFlags.Public).Invoke(null, new string[] { reader.ReadToEnd() });
               }
           }
           catch(Exception)
           {

           }
       }
   }
Posted
Comments
Tom Wauters 23-Sep-13 0:03am
   
What don't you understand?
ArunRajendra 23-Sep-13 0:05am
   
What you want to know. Be specific?

You are listing out a set of methods where the method parameters are of type string.
   
Usually, we do not "explain" the code taken by our inquirers somewhere on the Web, because doing so means loosing a huge amount of time for nothing. There is too much trash out there, so why trying to explain it all. On this forum, one of out experts demonstrated that "explanation" of a single code line could take a whole article. Remember, we know nothing about your background and knowledge and don't know how detailed the "explanation" should be. In other words, the notion of explanation is not well-defined. Anyway, not well enough to start such explanation. You rather need some general instruction on learning programming and APIs you need.

You are supposed to write your own code, and, if you are stuck, ask for help. This would be much more effective. If you really want to look how others write code, do it, but, to get explanation 1) ask the author of it, 2) find each and every word you don't understand and find it in the original documentation (in this case, MSDN). Actually, you need to do item #2 first, before asking any questions.

By the way, the code sample you show is almost self-explanatory. At the same time, in practice, reflecting the type by its name is a very bad technique, but this is only one line. Anyway, not understanding such code is rather a result of some laziness, which is normally a pretty good thing, but not in this case. You should not have looked at the sample code at all, before you read through the documentation. Only then reading someone else's code could be useful, to certain extent.

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900