Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中使用名称调用方法#_C#_Reflection_Methods - Fatal编程技术网

C# 在C中使用名称调用方法#

C# 在C中使用名称调用方法#,c#,reflection,methods,C#,Reflection,Methods,我的应用程序中有许多“作业”,每个作业都有一个需要调用的方法列表,以及它的参数。实际上,包含以下对象的列表称为: string Name; List<object> Parameters; 我的方法对象如下所示: Name = TestMethod Parameters = "astring", 3 public class Test { public void Hello(string s) { Console.WriteLine("hello " + s); } }

我的应用程序中有许多“作业”,每个作业都有一个需要调用的方法列表,以及它的参数。实际上,包含以下对象的列表称为:

string Name;
List<object> Parameters;
我的方法对象如下所示:

Name = TestMethod
Parameters = "astring", 3
public class Test
{
    public void Hello(string s) { Console.WriteLine("hello " + s); }
}

...

{
     Test t = new Test();
     typeof(Test).GetMethod("Hello").Invoke(t, new[] { "world" }); 

     // alternative if you don't know the type of the object:
     t.GetType().GetMethod("Hello").Invoke(t, new[] { "world" }); 
}

有可能这样做吗?我想反射将是这里的关键。

如果您正在使用.NET Framework 4,请查看,否则,然后调用
MethodInfo
Invoke
当然,您可以这样做:

Name = TestMethod
Parameters = "astring", 3
public class Test
{
    public void Hello(string s) { Console.WriteLine("hello " + s); }
}

...

{
     Test t = new Test();
     typeof(Test).GetMethod("Hello").Invoke(t, new[] { "world" }); 

     // alternative if you don't know the type of the object:
     t.GetType().GetMethod("Hello").Invoke(t, new[] { "world" }); 
}
Invoke()的第二个参数是包含要传递给方法的所有参数的对象数组

假设所有方法都属于同一个类,您可以拥有该类的方法,如下所示:

public void InvokeMethod(string methodName, List<object> args)
{
    GetType().GetMethod(methodName).Invoke(this, args.ToArray());
}
public void InvokeMethod(字符串方法名,列表参数)
{
GetType().GetMethod(methodName).Invoke(this,args.ToArray());
}

使用。应该使用
System.Reflection

使用.NET 2.0。如果您不得不求助于反射,可能有更好的方法来完成您的任务。这可能需要更多的架构,但它是可行的

请记住,拥有更多的代码并不是一件坏事——特别是当它能够提高代码的可读性和可管理性时。对于大多数人来说,反射是很难理解的,并且您会失去大部分编译时类型安全性。在您的示例中,对于计划调用的每个方法,您可能只需要使用switch语句和不同的对象就可以了。e、 g

// Have some object hold the type of method it plans on calling.
enum methodNames
{
   Method1,
   Method2
}

...
class someObject
{
   internal methodNames methodName {get; set;}
   internal object[] myParams;
}
...

//  Execute your object based on the enumeration value it references.
switch(someObject1.methodName)
{
   case Method1:
      Test.Method1(Int32.Parse(someObject1.myParams[0].ToString),someObject1.myParams[1].ToString());
      break;
   ...
}

如果您知道您只有一组独特的方法可供调用,为什么不提前设置好自己呢?

快去救援吧<代码>PM>安装软件包dnpextensions

一旦在项目中有了该包,所有对象现在都应该有一个
.InvokeMethod()
扩展名,该扩展名将方法名作为字符串和任意数量的参数

从技术上讲,方法名称使用“魔术字符串”,因此,如果您希望强类型地键入方法字典,您可以制作MethodInfo类型的键,并按如下方式获取它们

MethodInfo[] methodInfos = typeof(MyClass).GetMethods();
var methods = new Dictionary<MethodInfo, Object[]>();
foreach (var item in methods)
   item.key.Invoke(null, item.value);  
   // 'null' may need to be an instance of the object that
   // you are calling methods on if these are not static methods.
然后你可以这样做

MethodInfo[] methodInfos = typeof(MyClass).GetMethods();
var methods = new Dictionary<MethodInfo, Object[]>();
foreach (var item in methods)
   item.key.Invoke(null, item.value);  
   // 'null' may need to be an instance of the object that
   // you are calling methods on if these are not static methods.
var方法=新字典();
foreach(方法中的var项)
item.key.Invoke(null,item.value);
//“null”可能需要是
//如果这些方法不是静态方法,则您正在调用上的方法。

或者,您可以使用我前面提到的dnpextensions对上述块进行一些修改。

如果在回答此问题时将.NET Framework 4视为新的或旧的标准,则添加此选项将很有帮助。这个答案目前的措辞并不表明
dynamic
是否也应该在4以上或4以上的.NET Framework上使用。