Home >Backend Development >C++ >Can C# Call a Function from a String Using Reflection?

Can C# Call a Function from a String Using Reflection?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-26 02:26:09831browse

Can C# Call a Function from a String Using Reflection?

In the string call function

PHP can call the function through the string, as shown below:

.NET could achieve similar functions?
<code class="language-php">$function_name = 'hello';
$function_name();

function hello() { echo 'hello'; }</code>

Answer:

Yes, .NET can use the reflection string to call the function. The following is the implementation method:

In the above code, the method of calling must have a public access decoration. If you need to call the non -public method, you can use the bindingflags parameter, as shown below:

The above is the detailed content of Can C# Call a Function from a String Using Reflection?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn