{"id":25453,"date":"2024-03-16T04:58:57","date_gmt":"2024-03-16T04:58:57","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/"},"modified":"2024-03-22T06:12:37","modified_gmt":"2024-03-22T06:12:37","slug":"how-to-invoke-a-method-using-c-reflection","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/","title":{"rendered":"How to invoke a method using C# reflection?"},"content":{"rendered":"<p>The steps for invoking a method through C# reflection are as follows:<\/p>\n<ol>\n<li>Include the System.Reflection namespace.<\/li>\n<li>Use the Type.GetType method to obtain the Type object of the class in which the method is to be called.<\/li>\n<li>You can use the Type.GetMethod method to retrieve the MethodInfo object of the method to be called. This can be done by providing information such as the method name and parameter types.<\/li>\n<li>Use the MethodInfo.Invoke method to call a method. Pass in the instance object to be called (if it is an instance method) and the method&#8217;s parameters (if there are any).<\/li>\n<\/ol>\n<p>Here is an example code demonstrating how to invoke a method using C# reflection.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">using<\/span> System;\r\n<span class=\"hljs-keyword\">using<\/span> System.Reflection;\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span>\r\n{\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">MyMethod<\/span>(<span class=\"hljs-params\"><span class=\"hljs-built_in\">string<\/span> message<\/span>)<\/span>\r\n    {\r\n        Console.WriteLine(<span class=\"hljs-string\">\"MyMethod: \"<\/span> + message);\r\n    }\r\n}\r\n\r\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\r\n{\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>()<\/span>\r\n    {\r\n        <span class=\"hljs-comment\">\/\/ \u83b7\u53d6MyClass\u7c7b\u7684Type\u5bf9\u8c61<\/span>\r\n        Type type = <span class=\"hljs-keyword\">typeof<\/span>(MyClass);\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u83b7\u53d6MyMethod\u65b9\u6cd5\u7684MethodInfo\u5bf9\u8c61<\/span>\r\n        MethodInfo method = type.GetMethod(<span class=\"hljs-string\">\"MyMethod\"<\/span>);\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u521b\u5efaMyClass\u7684\u5b9e\u4f8b<\/span>\r\n        MyClass myObject = <span class=\"hljs-keyword\">new<\/span> MyClass();\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u8c03\u7528MyMethod\u65b9\u6cd5<\/span>\r\n        method.Invoke(myObject, <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">object<\/span>[] { <span class=\"hljs-string\">\"Hello World!\"<\/span> });\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>When running the code above, the output will be:<\/p>\n<pre class=\"post-pre\"><code>MyMethod: Hello World!\r\n<\/code><\/pre>\n<p>Note: If the method you want to call is a static method, you can pass in null as the instance object. If the method is private, you can use the BindingFlags.NonPublic flag to obtain the MethodInfo object of the method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The steps for invoking a method through C# reflection are as follows: Include the System.Reflection namespace. Use the Type.GetType method to obtain the Type object of the class in which the method is to be called. You can use the Type.GetMethod method to retrieve the MethodInfo object of the method to be called. This can [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-25453","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to invoke a method using C# reflection? - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to invoke a method using C# reflection?\" \/>\n<meta property=\"og:description\" content=\"The steps for invoking a method through C# reflection are as follows: Include the System.Reflection namespace. Use the Type.GetType method to obtain the Type object of the class in which the method is to be called. You can use the Type.GetMethod method to retrieve the MethodInfo object of the method to be called. This can [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T04:58:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T06:12:37+00:00\" \/>\n<meta name=\"author\" content=\"Liam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Liam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/\"},\"author\":{\"name\":\"Liam\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\"},\"headline\":\"How to invoke a method using C# reflection?\",\"datePublished\":\"2024-03-16T04:58:57+00:00\",\"dateModified\":\"2024-03-22T06:12:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/\"},\"wordCount\":175,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/\",\"name\":\"How to invoke a method using C# reflection? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T04:58:57+00:00\",\"dateModified\":\"2024-03-22T06:12:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to invoke a method using C# reflection?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\",\"name\":\"Liam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"caption\":\"Liam\"},\"sameAs\":[\"http:\/\/Wilson\"],\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to invoke a method using C# reflection? - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/","og_locale":"en_US","og_type":"article","og_title":"How to invoke a method using C# reflection?","og_description":"The steps for invoking a method through C# reflection are as follows: Include the System.Reflection namespace. Use the Type.GetType method to obtain the Type object of the class in which the method is to be called. You can use the Type.GetMethod method to retrieve the MethodInfo object of the method to be called. This can [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T04:58:57+00:00","article_modified_time":"2024-03-22T06:12:37+00:00","author":"Liam","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Liam","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/"},"author":{"name":"Liam","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671"},"headline":"How to invoke a method using C# reflection?","datePublished":"2024-03-16T04:58:57+00:00","dateModified":"2024-03-22T06:12:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/"},"wordCount":175,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/","name":"How to invoke a method using C# reflection? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T04:58:57+00:00","dateModified":"2024-03-22T06:12:37+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-invoke-a-method-using-c-reflection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to invoke a method using C# reflection?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671","name":"Liam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","caption":"Liam"},"sameAs":["http:\/\/Wilson"],"url":"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/25453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=25453"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/25453\/revisions"}],"predecessor-version":[{"id":59558,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/25453\/revisions\/59558"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=25453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=25453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=25453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}