C#에서 문자열을 통해 함수 호출
PHP는 문자열에 저장된 함수 이름으로 함수를 호출할 수 있습니다. 예:
<code class="language-php">$function_name = 'hello'; $function_name(); function hello() { echo 'hello'; }</code>
이 기능이 .NET에서 가능합니까?
그렇습니다. 리플렉션을 사용하여 이 기능을 구현할 수 있습니다. 방법은 다음과 같습니다.
<code class="language-csharp">Type thisType = this.GetType(); MethodInfo theMethod = thisType.GetMethod(TheCommandString); theMethod.Invoke(this, userParameters);</code>
호출되는 메소드에는 공개 액세스 수정자가 있어야 합니다. 비공개 메서드의 경우 BindingFlags 매개 변수를 사용하세요.
<code class="language-csharp">Type thisType = this.GetType(); MethodInfo theMethod = thisType .GetMethod(TheCommandString, BindingFlags.NonPublic | BindingFlags.Instance); theMethod.Invoke(this, userParameters);</code>
위 내용은 C#은 문자열에서 함수를 호출 할 수 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!