解释代码反思 [英] explain the codes Reflection

查看:62
本文介绍了解释代码反思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)
           {

           }
       }
   }

推荐答案

您列出了一组方法,其中方法参数的类型为字符串。
You are listing out a set of methods where the method parameters are of type string.

通常,我们不会解释我们的查询者在网络上的某些地方所采取的代码,因为这样做意味着浪费了大量的时间。那里有太多的垃圾,所以为什么要解释这一切。在这个论坛上,其中一位专家证明,单一代码行的解释可能需要整篇文章。请记住,我们对您的背景和知识一无所知,也不知道解释的详细程度。换句话说,解释的概念没有明确定义。无论如何,还不够开始这样的解释。您需要一些关于学习所需的API和API的一般性指导。



您应该编写自己的代码,如果遇到困难,请求帮助。这会更有效。如果你真的想看看其他人如何编写代码,那么就去做,但是,要获得解释1)问问作者,2)找到你不理解的每一个单词并在原始文档中找到它(在这种情况下) ,MSDN)。实际上,在提出任何问题之前,你需要先做第2项。



顺便说一下,你展示的代码示例几乎是不言自明的。同时,在实践中,通过名称反映类型是一种非常糟糕的技术,但这只是一条线。无论如何,不​​理解这些代码是一些懒惰的结果,这通常是一件非常好的事情,但在这种情况下并非如此。在阅读文档之前,您根本不应该查看示例代码。只有这样才能在某种程度上阅读别人的代码。



-SA
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


这篇关于解释代码反思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆