使用实体类参数调用MethodInfo



我有一个类名"BusinessError"。我正试图从控制台应用程序调用这个类(使用反射(。出现错误。。(我多次清理visual studio或重新启动(

Message="无法将'RestService.RequestGetOrderDetail'类型的对象转换为'RestService[RequestGetOrderDetail'类型。"但是当ı尝试调用BusinessHasNoError类时没有错误。。

MethodInfo如何使用实体类参数调用(无字符串,带参数的int(。。

public class BusinessError
{
public ResponseGetOrderDetail Test1(RequestGetOrderDetail requestGetOrderDetail)
{
ResponseGetOrderDetail responseGetOrderDetail = new ResponseGetOrderDetail(
requestGetOrderDetail);
return responseGetOrderDetail;
}
}


public class BusinessNoError
{
public ResponseGetOrderDetail Test1(string id)
{
ResponseGetOrderDetail responseGetOrderDetail = new ResponseGetOrderDetail(id);
return responseGetOrderDetail;
}
}

带有


public string Execute(DllInfo dllInfo)
{
object[] parameterObject = new object[1];
parameterObject[0] = "O19082900055";
string dllsPath = @"C:SchedulerDLLs" + dllInfo.Dll;
string nameSpace = dllInfo.NameSpace;
string className = dllInfo.Class;
string methodName = dllInfo.Method;
object result = string.Empty;
RequestGetOrderDetail requestGetOrderDetail = new RequestGetOrderDetail();
requestGetOrderDetail.OrderID = "O19082900055";
parameterObject[0] = new RequestGetOrderDetail() { OrderID = "O19082900055" };
try
{
Assembly assembly = Assembly.LoadFrom(dllsPath);
Type typeInstance = assembly.GetType(nameSpace + "." + className);
if (typeInstance != null)
{
MethodInfo methodInfo = typeInstance.GetMethod(methodName);
ParameterInfo[] parameterInfo = methodInfo.GetParameters();
object classInstance = Activator.CreateInstance(typeInstance, null);
if (parameterInfo.Length == 0)
{
result = methodInfo.Invoke(classInstance, null);
}
else
{
//I want to send requestGetOrderDetail object to  methodInfo.Invoke !!!!!
//result = methodInfo.Invoke(classInstance, requestGetOrderDetail);  
**result = methodInfo.Invoke(classInstance, parameterObject);**
}
}
}
catch (Exception ex)
{
throw;
}
return result.ToString();
}

文件示例

Invoke将参数作为对象[];(没有其他内容(所以你的电话应该是
Invoke(classInstance, new object[] { new RequestGetOrderDetail() {OrderId=..}; });

相关内容

  • 没有找到相关文章

最新更新