155

私はphpであなたが次のような呼び出しを行うことができることを知っています:

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

function hello() { echo 'hello'; }

これは.Netで可能ですか?

4

5 に答える 5

292

はい。反射を使用できます。このようなもの:

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

上記のコードでは、呼び出されるメソッドには access modifier が必要publicです。非パブリック メソッドを呼び出す場合は、次のようなBindingFlagsパラメーターを使用する必要がありBindingFlags.NonPublic | BindingFlags.Instanceます。

Type thisType = this.GetType();
MethodInfo theMethod = thisType
    .GetMethod(TheCommandString, BindingFlags.NonPublic | BindingFlags.Instance);
theMethod.Invoke(this, userParameters);
于 2009-02-12T04:53:33.297 に答える
80

リフレクションを使用してクラス インスタンスのメソッドを呼び出し、動的メソッド呼び出しを行うことができます。

実際のインスタンス (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);
于 2009-02-12T04:57:56.063 に答える
40
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();
        }
    }
于 2009-02-12T04:57:39.980 に答える
3
このコードは私のコンソール .Net アプリケーションで動作します
class Program
{
    static void Main(string[] args)
    {
        string method = args[0]; // get name method
        CallMethod(method);
    }
    
    public static void CallMethod(string method)
    {
        try
        {
            Type type = typeof(Program);
            MethodInfo methodInfo = type.GetMethod(method);
            methodInfo.Invoke(method, null);
        }
        catch(Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Console.ReadKey();
        }
    }
    
    public static void Hello()
    {
        string a = "hello world!";
        Console.WriteLine(a);
        Console.ReadKey();
    }
}
于 2020-07-14T22:10:42.077 に答える
2

ちょっとした接線 -- (ネストされた!) 関数を含む式文字列全体を解析して評価する場合は、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デリゲート内で既存の関数を呼び出します。

于 2014-12-05T15:58:12.903 に答える