TypeLoadException

  • Thread starter Thread starter Aberro-Bill
  • Start date Start date
A

Aberro-Bill

Hi All,
I have code that loads a strongly named assembly using "Assembly.LoadFrom()"
and then finds all the types in the assembly using "GetTypes()". The code
has worked flawlessly in .NET 1.1 both on a development box and a clean box.
With .NET 2 it works on a development box, but NOT a clean box (XP pro, SP
2, .NET 1.1 and 2, all updates to this date). If I install the .NET 2 SDK
on my clean box it will work.
When it fails the message lists all the types in the assembly, so I know the
assembly loaded.

Why does this work on a development box or a box with the .NET 2 SDK, but
not a clean box?

What do I do to make it work on a clean box?

Thanks,
Bill

Here is a code snippet:

Assembly MyAssembly = Assembly.LoadFrom( FileName );
Type[] MyTypes = null;
try
{
MyTypes = MyAssembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
string msg = "";

foreach (Exception e in ex.LoaderExceptions)
{
if (e.Message != null) msg += e.Message + Environment.NewLine;
}
msg = "GetMyTypes() threw a TypeLoadException" + Environment.NewLine +
msg;
MessageBox.Show( msg );
}
 
Hi Bill,

If the app fails on loading the assembly file, could you please post the
exception message and stack trace here, so that I can do more research on
it.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi Kevin,

The app appears to succsssfully load the assembly file. The failure occurs
when I try to extract the types in it.
The exact type of the exception thrown by GetTypes is
"ReflectionTypeLoadException". Here is the list from that exception and the
stack trace.
Note: the loaded assembly has exactly 2 types in it and both their types
failed to load. Both types are derived from a base type "Observer" from a
dll in the GAC.
This works on a development box (app built with Visual Studio 2005), a box
with .NET 2 and .NET 2 SDK, but not a box with just .NET 2 (which is what my
customers will have)
My test boxes are XP SP2 Virtual PCs, all security patches up to date.
I have looked at the fusion log and it does not seem to show anything
unusual.
How can I get more diagnostic info about why GetTypes() is failing?

Bill

GetTypes() in LoadObservers threw a
System.Reflection.ReflectionTypeLoadException
Could not load type 'ContraTest.Default' from assembly 'Observers,
Version=1.5.0.0, Culture=neutral, PublicKeyToken=54150cdd1a8a7687'.
Could not load type 'ContraTest.NativeMDI' from assembly 'Observers,
Version=1.5.0.0, Culture=neutral, PublicKeyToken=54150cdd1a8a7687'.

Stack Trace:
at System.Reflection.Module.GetTypesInternal(StackCrawlMark& stackMark)
at System.Reflection.Assembly.GetTypes()
at ContraTest.Project.LoadObservers()
 
Hi Bill,

The Observer interface is in the visual J# library, which you might need to
install the Visual J# redistribution page. You can download the 2.0 version
from the following link:

http://www.microsoft.com/downloads/details.aspx?FamilyID=f72c74b3-ed0e-4af8-
ae63-2f0e42501be1&DisplayLang=en

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi Kevin,

ContraTest.Observer is my base class and these classes are namespaced so
there is no conflict with visual J# and there is no J# code - this
application is C# and some managed C++ but there is no C++ involved in this
part of the code. The types that fail to load are in my namespace ie
"ContraTest.Default".

So I don't see how your commment that we need the J# redist.

Bill
 
Hi Bill,

I mistake it for the Observer class in J#. On that clean machine, has your
strong named assembly been installed to GAC properly? In other words, you
can check in c:\windows\assembly folder to see if that assembly(with
correct name, version, culture and public key token) has been installed.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Yes, I have checked that the strongly named assembly is GACd, is the correct
version, etc.
I know it can work because if I take the clean machine with my software
installed and then install the .NET 2 SDK my software starts working just
fine and I have not touched the installation of my software. The only thing
different is I installed the .NET 2 SDK.

Thanks,
Bill
 
Hi Bill,

This seems to be a little weird. Could you please attach your assembly and
some code for me to test?

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top