Thanks for your reply.
My problem is as follows:

I have an assembly that contains a method that returns a class that is
also defined in that assembly.
The assembly is in the GAC.

However, for my application, I want to use Assembly.LoadFrom() to load
the assembly from a file path also. 
So my application code calls Assembly.LoadFrom() on the assembly,
executes the method and the returned class is used in the calling code. 
However, I believe the calling code loads the assembly from the GAC in
the Load context (via a static reference) in order to define a variable
of the class returned from the method, and Assembly.LoadFrom() will load
the assembly into the LoadFrom context. The class returned from the
method is not compatible with the class variable defined in the calling
code as they are in different contexts and I get the Invalid Cast
exception.

I would like to be able to load the assembly using it's file path, but I
want it to be in the Load context so the calling code will work. 
I realise, this may sound like a strange request, the issue is that the
assembly is a 3rd party assembly/add in and we have no control over
whether it will be in the GAC or not. : )

Any advice is appreciated.

Charles.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Fabian Schmied
Sent: 16 May 2007 12:06
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Using Assembly.LoadFrom instead of
Assembly.Load()

> I am having the problems mentioned in this article when using
> Assembly.LoadFrom():
>
>
http://blogs.msdn.com/suzcook/archive/2004/06/02/debugging-an-invalidcas
> texception.aspx
>
> My question is, how can I change my code to use Assembly.Load(), to
> avoid these problems, when I only have the file path for the assembly?
I
> am running .NET 1.1.

If I understand correctly, the problem is that you are loading the
assembly twice, but from different paths. Switching to Assembly.Load()
won't help there much, I guess, you should probably investigate why
you are loading the assembly twice and remove that problem.

Anyway, to use Assembly.Load instead of Assembly.LoadFrom, the
assembly must either be in the GAC or in the application directory.
Therefore, I guess it would work to just copy the files to the
application directory and then use Assembly.Load _if_ your application
has the necessary rights to write to the application directory.

Fabian

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com

________________________________________________________________________
This e-mail has been scanned for all viruses by MessageLabs.
________________________________________________________________________

________________________________________________________________________
This e-mail has been scanned for all viruses by MessageLabs.

Singularity operates globally through its offices in New York, London, 
Singapore, Ireland and India. Singularity Limited is incorporated in the United 
Kingdom with Registration Number NI 31519 and its Registered Office at 100 
Patrick Street, Derry, BT48 7EL, United Kingdom.
________________________________________________________________________

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to