Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing Method Exception on inheritance

Tags:

c#

datatable

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.

like image 486
Jose3d Avatar asked Sep 05 '26 22:09

Jose3d


1 Answers

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);
}
like image 99
Marc Gravell Avatar answered Sep 07 '26 10:09

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!