Jose3d
Jose3d

Reputation: 9277

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.

Upvotes: 3

Views: 1045

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062975

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);
}

Upvotes: 6

Related Questions