{"id":74,"date":"2009-01-06T13:45:02","date_gmt":"2009-01-06T11:45:02","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=74"},"modified":"2009-08-21T09:00:08","modified_gmt":"2009-08-21T07:00:08","slug":"better-typegetmethod","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=74","title":{"rendered":"Better Type.GetMethod()"},"content":{"rendered":"<p>Getting a Generic MethodInfo with &#8220;Type.GetMethod()&#8221; is not really possible. That&#8217;s why I wrote this little Function which helps finding any Methods and return it&#8217;s correct MethodInfo on which you then can Invoke on.<!--more--><\/p>\n<pre class=\"brush: csharp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Finds a Method with the given Specifications\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;example&gt;\r\n\/\/\/ MethodInfo mi;\r\n\/\/\/ ParameterInfo&#x5B;] parameters;\r\n\/\/\/ FindMethod(this.GetType(), &quot;Save&quot;, new Type&#x5B;] { typeof(int) }, new Type&#x5B;] { typeof(DataRow) }, out mi, out parameters);\r\n\/\/\/ mi.Invoke(this, new object&#x5B;] { row });\r\n\/\/\/ &lt;\/example&gt;\r\n\/\/\/ &lt;param name=&quot;type&quot;&gt;The Type which holds the Method&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;methodName&quot;&gt;The Name of the Method&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;typeArguments&quot;&gt;Generic Type List or Null if not Generic&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;parameterTypes&quot;&gt;Parameter Type List or Null if no Parameters&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;methodInfo&quot;&gt;The returned MethodInfo&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;parameters&quot;&gt;The returned ParameterInfo List&lt;\/param&gt;\r\n\/\/\/ &lt;returns&gt;True if found, false otherwise&lt;\/returns&gt;\r\nprivate static bool FindMethod(Type type, string methodName, Type&#x5B;] typeArguments, Type&#x5B;] parameterTypes, out MethodInfo methodInfo, out ParameterInfo&#x5B;] parameters)\r\n{\r\n    \/\/ Reset the Out Parameters\r\n    methodInfo = null;\r\n    parameters = null;\r\n\r\n    if (typeArguments == null)\r\n    {\r\n        \/\/ Non-Generic Method\r\n        methodInfo = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null);\r\n    }\r\n    else\r\n    {\r\n        \/\/ Generic Method\r\n        MethodInfo&#x5B;] methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);\r\n        \/\/ Loop thru all Methods\r\n        foreach (MethodInfo method in methods)\r\n        {\r\n            if (method.Name != methodName)\r\n            {\r\n                \/\/ Name does not match\r\n                continue;\r\n            }\r\n\r\n            if (!method.IsGenericMethod)\r\n            {\r\n                \/\/ Non-Generic\r\n                continue;\r\n            }\r\n\r\n            \/\/ Compare the Method Parameters\r\n            bool paramsOk = false;\r\n            if (method.GetParameters().Length == parameterTypes.Length)\r\n            {\r\n                \/\/ Count Matches\r\n                paramsOk = true;\r\n                \/\/ Check each Type\r\n                for (int i = 0; i &lt; method.GetParameters().Length; i++)\r\n                {\r\n                    if (method.GetParameters()&#x5B;i].ParameterType != parameterTypes&#x5B;i])\r\n                    {\r\n                        \/\/ Parameter Type doesn't Match\r\n                        paramsOk = false;\r\n                        break;\r\n                    }\r\n                }\r\n            }\r\n            if (!paramsOk)\r\n            {\r\n                \/\/ Parameters didn't match\r\n                continue;\r\n            }\r\n\r\n            \/\/ Check the Generic Arguments\r\n            bool argsOk = false;\r\n            if (method.GetGenericArguments().Length == typeArguments.Length)\r\n            {\r\n                \/\/ Count Matches\r\n                argsOk = true;\r\n                \/\/ TODO: Check for &quot;where&quot; Limitation in the Generic Definition\r\n            }\r\n            if (!argsOk)\r\n            {\r\n                \/\/ Generic Arguments didn't match\r\n                continue;\r\n            }\r\n\r\n            \/\/ If we're here, we got the right Method\r\n            methodInfo = method.MakeGenericMethod(typeArguments);\r\n            break;\r\n        }\r\n    }\r\n    if (null == methodInfo)\r\n    {\r\n        return false;\r\n    }\r\n    else\r\n    {\r\n        parameters = methodInfo.GetParameters();\r\n        return true;\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Getting a Generic MethodInfo with &#8220;Type.GetMethod()&#8221; is not really possible. That&#8217;s why I wrote this little Function which helps finding any Methods and return it&#8217;s correct MethodInfo on which you then can Invoke on.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,3],"tags":[30,67,41,29],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-roemer","4":"has-more-link","5":"post-74","7":"format-standard","8":"category-csharp","9":"category-programming","10":"post_tag-generic","11":"post_tag-getmethod","12":"post_tag-methodinfo","13":"post_tag-type"},"acf":[],"views":8237,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/74"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}