Ich weiß, dass Sie in PHP einen Anruf tätigen können wie:
$function_name="hello";
$function_name();
function hello() { echo 'hello'; }
Ist dies in .Net möglich?
Ich weiß, dass Sie in PHP einen Anruf tätigen können wie:
$function_name="hello";
$function_name();
function hello() { echo 'hello'; }
Ist dies in .Net möglich?
Ottobar
Jawohl. Sie können Reflexion verwenden. Etwas wie das:
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheCommandString);
theMethod.Invoke(this, userParameters);
Mit dem obigen Code muss die aufgerufene Methode einen Zugriffsmodifikator haben public
. Wenn eine nicht-öffentliche Methode aufgerufen wird, muss die verwendet werden BindingFlags
Parameter, z BindingFlags.NonPublic | BindingFlags.Instance
:
Type thisType = this.GetType();
MethodInfo theMethod = thisType
.GetMethod(TheCommandString, BindingFlags.NonPublic | BindingFlags.Instance);
theMethod.Invoke(this, userParameters);
Sie können Methoden einer Klasseninstanz mithilfe von Reflektion aufrufen, indem Sie einen dynamischen Methodenaufruf durchführen:
Angenommen, Sie haben eine Methode namens hallo in einer tatsächlichen Instanz (dies):
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);
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();
}
}
Dieser Code funktioniert in meiner .Net-Konsolenanwendung
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();
}
}
Eine leichte Tangente — wenn Sie eine ganze Ausdruckszeichenfolge analysieren und auswerten möchten, die (verschachtelte!) Funktionen enthält, ziehen Sie NCalc in Betracht (http://ncalc.codeplex.com/ und Nuget)
Ex. leicht modifiziert aus der Projektdokumentation:
// 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());
Und innerhalb der EvaluateFunction
Delegaten würden Sie Ihre vorhandene Funktion aufrufen.