简体   繁体   中英

Missing Method Exception on inheritance

i have the following class that inherits from 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:

ExcelStaticDataTable table=new ExcelStaticDataTable("table1");
table.Clone();

Both pieces of codes are in a different dll's just for clarify. 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()); as part of Clone() . That requires a public parameterless constructor in the default usage. Otherwise it throws a MissingMethodException .

Update: your update showing the stack-trace confirms this.

I suspect you can fix this by overriding the CreateInstance method:

protected override DataTable CreateInstance()
{
    return new ExcelStaticDataTable(TableName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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