[英]Missing Method Exception on inheritance
i have the following class that inherits from DataTable: 我有以下继承自DataTable的类:
public class ExcelStaticDataTable : DataTable
{
public List<ExcelStaticDataTable> SubTables { get; set; }
public ExcelStaticDataTable(string tableName): base(tableName)
{
SubTables = new List<ExcelStaticDataTable>();
}
}
Do you know why im getting a MissingMethodException
"parameterless constructor defined for this object" when i do the following: 你知道为什么我在执行以下操作时获取MissingMethodException
“为此对象定义的无参数构造函数”:
ExcelStaticDataTable table=new ExcelStaticDataTable("table1");
table.Clone();
Both pieces of codes are in a different dll's just for clarify. 这两段代码都在不同的dll中,只是为了澄清。 and here the stacktrace: 这里的堆栈跟踪:
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Data.DataTable.CreateInstance()
at System.Data.DataTable.Clone(DataSet cloneDS)
at System.Data.DataTable.Clone()
at System.Data.DataTable.Copy()
at ..........cs:line 35
Thanks. 谢谢。
I suspect the other code is using object newObj = Activator.CreateInstance(GetType());
我怀疑其他代码是使用object newObj = Activator.CreateInstance(GetType());
as part of Clone()
. 作为Clone()
一部分。 That requires a public parameterless constructor in the default usage. 这需要默认用法中的公共无参数构造函数。 Otherwise it throws a MissingMethodException
. 否则会抛出MissingMethodException
。
Update: your update showing the stack-trace confirms this. 更新:显示堆栈跟踪的更新确认了这一点。
I suspect you can fix this by overriding the CreateInstance
method: 我怀疑你可以通过覆盖CreateInstance
方法来解决这个问题:
protected override DataTable CreateInstance()
{
return new ExcelStaticDataTable(TableName);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.