简体   繁体   English

通过名称调用函数并在C#中传递hashtable参数

[英]Call function by name and pass hashtable parameter in C#

I have a function that calls other functions dynamically via name. 我有一个通过名称动态调用其他函数的函数。 I need to pass a hashtable to the function being called. 我需要将哈希表传递给被调用的函数。 I have the code working in VB.Net but while trying to convert it to C# I am running into a error when I try to pass the hashtable as a parameter. 我有在VB.Net中工作的代码,但是在尝试将其转换为C#时,尝试将哈希表作为参数时遇到错误。 Can someone explain what is happening and how I can resolve it? 有人可以解释正在发生的事情以及如何解决吗?

This is the working VB.Net code: 这是有效的VB.Net代码:

Dim objTF As New ThreadFunctions 
Dim objResults As Object = CallByName(objTF, htParameters.Item("strMethodName"), CallType.Get, htParameters) 

Here is the C# code: 这是C#代码:

ThreadFunctions objTF = new ThreadFunctions();
Type objType = objTF.GetType();
MethodInfo miMethod = objType.GetMethod(htParameters["strMethodName"].ToString());
object objResults = miMethod.Invoke(objTF, htParameters); //This line has the issue

Error 1 The best overloaded method match for 'System.Reflection.MethodBase.Invoke(object, object[])' has some invalid arguments 错误1'System.Reflection.MethodBase.Invoke(object,object [])'的最佳重载方法匹配具有一些无效的参数

Try 尝试

object objResults = miMethod.Invoke(objTF, (object)htParameters);

As the params second argument threats the hashtable wrong. 作为params第二个参数威胁哈希表错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM