我有以下C#类和2个构造函数:
public class CSVImport_HR_Standard : CSVImport
{
int fPropertyID;
public CSVImport_HR_Standard()
{
}
public CSVImport_HR_Standard(string oActivationParams)
{
this.fPropertyID = Convert.ToInt32(oActivationParams);
}和父类:
public class CSVImport
{没有任何构造函数。
类是从以下方法调用的:
private object CreateCommandClassInstance(string pCommandClass, string pActivationParams.ToArray())
{
List<object> oActivationParams = new List<object>();
// In the current implementation we assume only one param of type int
if (pActivationParams != "")
{
Int32 iParam = Convert.ToInt32(pActivationParams);
oActivationParams.Add(iParam);
}
object oObject = Activator.CreateInstance(Type.GetType("GTS." + pCommandClass), oActivationParams);
return(oObject);
}哪里
pCommandClass = GTS.CSVImport_HR_Standard但我得到以下错误:
Constructor on type 'GTS.CSVImport_HR_Standard' not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: Constructor on type 'GTS.CSVImport_HR_Standard' not found.据我所知,构造函数是正确的,它传入了所有正确的参数,那么为什么它会给我这个错误呢?
根据我所读到的,我最好的猜测是它与这句话有关:
object oObject = Activator.CreateInstance(Type.GetType("GTS." + pCommandClass), oActivationParams);但我不确定是什么导致了问题,因为似乎构造函数是正确的?
发布于 2015-02-24 21:03:13
您的主要问题是在CreateInstance方法中使用List<object>作为第二个参数。这使得该方法搜索具有签名(List<object>)的构造函数,而不是其中元素的类型。
您必须调用ToArray才能调用该方法的正确重载(它现在调用:
object oObject = Activator.CreateInstance( Type.GetType("GTS." + pCommandClass)
, oActivationParams.ToArray()
);此外,请确保使用if (!string.IsNullOrEmpty(pActivationParams))而不是if (pActivationParams != "")。
发布于 2015-02-24 21:39:16
问题是它将数组转换为参数列表,并逐个传递它们。
为了解决这个问题,我对构造函数执行了以下操作:
public CSVImport_HR_Standard(params object[] oActivationParams)
{
this.fPropertyID = Convert.ToInt32(oActivationParams[0]);
}并按如下方式传入:
object oObject = Activator.CreateInstance(Type.GetType("GTS." + pCommandClass), oActivationParams.ToArray());https://stackoverflow.com/questions/28696364
复制相似问题