Exception when instantiating an object derived from a class that has > 128 bytes of value types (lon

A

Andrew

Hi. I'm fairly new to Compact Framework and have run into a problem that
has me scratching my head. Hopefully it's just ignorance on my part, but
I'd appreciate any help.

My problem has to do with inheritance. I have a base class (MyBase) with >
128 bytes of value types (doubles, longs, structs, etc). I can instantiate
an object of this type without any problems.

I also have a derived class (MyDerived) that derives from it. When I
instantiate an object of this type I get a System.TypeLoad.Exception. What
puzzles me is that when I modify the base class to declare <= 128 bytes of
value types, the exception goes away. To make things more interesting, if I
instantiate an object of the derived type and assign it to a base class
object (see below), I get a different exception
(System.MissingMethod.Exception).

Is there a limitation in .NET CF that I am not aware of?

I've tried this code on a regular .NET Framework app and it works fine.

I've boiled everything down to a simple test case below.

public Form1()
{
InitializeComponent();
//MyBase x = new MyBase(); // ok
//MyBase y = new MyDerived(); // System.MissingMethodException
MyDerived z = new MyDerived(); // System.TypeLoadException
}

public class MyBase
{
// Declaring more than 128 bytes here causes an exception when a
// derived class is instantiated.
// Not a problem if you dynamically allocate (array) though.
public long dummy1;
public long dummy2;
public long dummy3;
public long dummy4;
public long dummy5;
public long dummy6;
public long dummy7;
public long dummy8;
public long dummy9;
public long dummy10;
public long dummy11;
public long dummy12;
public long dummy13;
public long dummy14;
public long dummy15;
public long dummy16;
public byte oneByte; // Comment this line out and everything
works fine.

public MyBase()
{
}
}
public class MyDerived: MyBase
{
public MyDerived(): base()
{
}
}
 
A

Alex Feinman [MVP]

I don't think this is ignorance. I think you just found a most interesting
bug. Good news is that it seems to be fixed in CF 2.0.
 
A

Alex Feinman [MVP]

A recommended workaround would be to add a field that is an object reference
(not a valuetype) so that it falls into the first 128 bytes of the object
data. E.g. add a dummy member of type Object to the parent class and place
it so that it is the first member of the class
 
A

Andrew

Thanks for the info. I got around it for now by changing a couple structs
into objects. If it becomes a problem
then I'll certainly use your workaround. I'll be switching to CF2.0 soon
enough though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top