복붙노트

C #에서 문자열에서 함수 호출

PHP

C #에서 문자열에서 함수 호출

나는 PHP에서 다음과 같은 호출을 할 수 있음을 알고있다.

$function_name = 'hello';
$function_name();

function hello() { echo 'hello'; }

NET에서 가능합니까?

해결법

  1. ==============================

    1.예. 리플렉션을 사용할 수 있습니다. 이 같은:

    예. 리플렉션을 사용할 수 있습니다. 이 같은:

    Type thisType = this.GetType();
    MethodInfo theMethod = thisType.GetMethod(TheCommandString);
    theMethod.Invoke(this, userParameters);
    
  2. ==============================

    2.리플렉션을 사용하여 동적 메서드 호출을 수행하여 클래스 인스턴스의 메서드를 호출 할 수 있습니다.

    리플렉션을 사용하여 동적 메서드 호출을 수행하여 클래스 인스턴스의 메서드를 호출 할 수 있습니다.

    실제 인스턴스 (this)에 hello라는 메소드가 있다고 가정합니다.

    string methodName = "hello";
    
    //Get the method information using the method info class
     MethodInfo mi = this.GetType().GetMethod(methodName);
    
    //Invoke the method
    // (null- no parameter for the method call
    // or you can pass the array of parameters...)
    mi.Invoke(this, null);
    
  3. ==============================

    3.

    class Program
        {
            static void Main(string[] args)
            {
                Type type = typeof(MyReflectionClass);
                MethodInfo method = type.GetMethod("MyMethod");
                MyReflectionClass c = new MyReflectionClass();
                string result = (string)method.Invoke(c, null);
                Console.WriteLine(result);
    
            }
        }
    
        public class MyReflectionClass
        {
            public string MyMethod()
            {
                return DateTime.Now.ToString();
            }
        }
    
  4. ==============================

    4.약간의 접선 - (중첩 된) 함수가 포함 된 전체 표현식 문자열을 구문 분석하고 평가하려면 NCalc (http://ncalc.codeplex.com/ 및 nuget)를 고려하십시오.

    약간의 접선 - (중첩 된) 함수가 포함 된 전체 표현식 문자열을 구문 분석하고 평가하려면 NCalc (http://ncalc.codeplex.com/ 및 nuget)를 고려하십시오.

    전의. 프로젝트 문서에서 약간 수정 :

    // the expression to evaluate, e.g. from user input (like a calculator program, hint hint college students)
    var exprStr = "10 + MyFunction(3, 6)";
    Expression e = new Expression(exprString);
    
    // tell it how to handle your custom function
    e.EvaluateFunction += delegate(string name, FunctionArgs args) {
            if (name == "MyFunction")
                args.Result = (int)args.Parameters[0].Evaluate() + (int)args.Parameters[1].Evaluate();
        };
    
    // confirm it worked
    Debug.Assert(19 == e.Evaluate());
    

    그리고 EvaluateFunction 델리게이트 내에서 기존 함수를 호출합니다.

  5. ==============================

    5.사실 저는 Windows Workflow 4.5를 개발 중입니다. 저는 statemachine에서 대리인을 성공한 메서드로 전달할 방법을 찾아야합니다. 내가 찾아야 할 유일한 방법은 위임자로 전달하려는 메서드의 이름을 가진 문자열을 전달하고 메서드 내에서 위임자로 문자열을 변환하는 것이 었습니다. 아주 좋은 대답. 감사. 이 링크 확인 https://msdn.microsoft.com/en-us/library/53cz7sc6(v=vs.110).aspx

    사실 저는 Windows Workflow 4.5를 개발 중입니다. 저는 statemachine에서 대리인을 성공한 메서드로 전달할 방법을 찾아야합니다. 내가 찾아야 할 유일한 방법은 위임자로 전달하려는 메서드의 이름을 가진 문자열을 전달하고 메서드 내에서 위임자로 문자열을 변환하는 것이 었습니다. 아주 좋은 대답. 감사. 이 링크 확인 https://msdn.microsoft.com/en-us/library/53cz7sc6(v=vs.110).aspx

  6. ==============================

    6.C #에서는 대리자를 함수 포인터로 만들 수 있습니다. 사용법에 대한 자세한 내용은 다음 MSDN 문서를 참조하십시오. http://msdn.microsoft.com/en-us/library/ms173171(VS.80).aspx

    C #에서는 대리자를 함수 포인터로 만들 수 있습니다. 사용법에 대한 자세한 내용은 다음 MSDN 문서를 참조하십시오. http://msdn.microsoft.com/en-us/library/ms173171(VS.80).aspx

        public static void hello()
        {
            Console.Write("hello world");
        }
    
       /* code snipped */
    
        public delegate void functionPointer();
    
        functionPointer foo = hello;
        foo();  // Writes hello world to the console.
    
  7. from https://stackoverflow.com/questions/540066/calling-a-function-from-a-string-in-c-sharp by cc-by-sa and MIT license