Inheritance and TypeLoadException

E

Erik J Sawyer

I apologize if this has already been addressed; I looked,
but couldn't find it.

My VS solution contains 3 projects: a class library
(containg the app core logic), and both a Windows and a
PocketPC front-end. The class library contains an
abstract database operations class, then each front-end
app creates derived classes that connect to the
appropriate DB (either SQL or SQL CE).

What I'm running into is that the CF gets upset using a
base class, in a class library, that references a type
not already in memory. For example, if my app starts,
and creates an instance of the SQLCE derived class, and
the base class has a member variable of type
System.Data.IDbConnection, I get a TypeLoadException. I
can work around this by having my app create a variable
of a type from the assembly involved.

Note:
- the base class assembly DOES have a reference to the
System.Data assembly
- this problem does not happen on Windows with the
full .Net
- this is not restricted to System.Data. I have also
reproduced the behavior with System.Diagnostics
- although the Knowledgebase indicates that SP1 fixes
something similar, it does NOT fix this issue

If someone can help, PLEASE!
Thanks,
Erik J Sawyer
 
E

Erik J Sawyer

OK, I was reading the CF Core Ref tonight and realized
where the problem was. Let me walk you through steps to
reproduce it; see if you spot where the problem is.

1. Create a new C# Solution (e.g. "InheritanceBugApp1").
For the primary Project, choose "Smart Device
Application", then select PocketPC, Windows Application.

2. Add a new Project to the Solution. Choose C#
projects, Class Library.

3. Add a private member variable to the first class in
the classlib (e.g. Class1). The code should look like:
///////////////////////////////////////////////////
public class Class1
{
private System.Data.IDbConnection idbc;
// ^ note the member variable type
public Class1()
{ }
}
///////////////////////////////////////////////////

4. Add a reference for ClassLibrary1 to
InheritanceBugApp1.

5. Add a new class, Class2, to InheritanceBugApp1. The
base class should be Class1:
///////////////////////////////////////////////////
public class Class2 : ClassLibrary1.Class1
{
public Class2()
{ }
}
///////////////////////////////////////////////////

6. Create an instance of Class2 in the form:
///////////////////////////////////////////////////
static void Main()
{
Class2 c2;
Application.Run(new Form1());
}
///////////////////////////////////////////////////
The program will now generate a TypeLoadException when
run. There are a couple of workarounds, the simplest
being to create a reference to the System.Data assembly
prior to the Class2 instantiation:
///////////////////////////////////////////////////
public class Form1 : System.Windows.Forms.Form
{
private System.Data.IDbConnection idbc;
...
}
///////////////////////////////////////////////////

You probably spotted my mistake in item 2: I created
a .Net class library, rather than a .Net CF class
library. When I fixed this, the problem went away. Dumb
mistake, but it took several days of tearing my hair out
to find it.

Hope this helps someone else. Maybe MS can look into a
solution for this in the next rev...

Thanks,
Erik J Sawyer
-----Original Message-----
Greetings Erik,

Could you possibly post your code, or if you like, I can contact you
offline to get a copy of your project. I would like to look a little
deeper into what you're seeing.

Jeremy Hance
.NET Compact Framework

This posting is provided "AS IS" with no warranties, and
confers no rights.
 

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