AW: [Mono-list] Assembly.LoadFrom() - Assembly.CreateInstance() - Activator.CreateInstanceFrom() ---> invalid cast

Jaroslaw Kowalski jaak@zd.com.pl
Tue, 27 Jan 2004 10:20:36 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0089_01C3E4BF.3483DF90
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I've enhanced the test code:

        Type t1 =3D Assembly.LoadFrom(@"A.1.dll").GetType("A.Y");
        Type t2 =3D Assembly.LoadFrom(@"A.2.dll").GetType("A.Y");
        Type t3 =3D typeof(A.Y);=20

        Console.WriteLine(t1.Assembly.EscapedCodeBase);
        Console.WriteLine(t2.Assembly.EscapedCodeBase);
        Console.WriteLine(t3.Assembly.EscapedCodeBase);

        Console.WriteLine(t1.AssemblyQualifiedName);
        Console.WriteLine(t2.AssemblyQualifiedName);
        Console.WriteLine(t3.AssemblyQualifiedName);

        Console.WriteLine(t1.GUID);
        Console.WriteLine(t2.GUID);
        Console.WriteLine(t3.GUID);

        Console.WriteLine(t1 =3D=3D t2);
        Console.WriteLine(t1 =3D=3D t3);
        Console.WriteLine(t2 =3D=3D t3);=20

and on .NET 1.0 it produces:

file:///D:/ttt/A.1.dll
file:///D:/ttt/A.1.dll
file:///D:/ttt/a.DLL
A.Y, a, Version=3D0.0.0.0, Culture=3Dneutral, PublicKeyToken=3Dnull
A.Y, a, Version=3D0.0.0.0, Culture=3Dneutral, PublicKeyToken=3Dnull
A.Y, a, Version=3D0.0.0.0, Culture=3Dneutral, PublicKeyToken=3Dnull
2c21befd-5a3f-3d21-ab48-1c6c1361dc0b
2c21befd-5a3f-3d21-ab48-1c6c1361dc0b
2c21befd-5a3f-3d21-ab48-1c6c1361dc0b
True
False
False

Note that LoadFrom("A.2.dll") caused A.1.dll to be loaded. Strange.

Jarek

  ----- Original Message -----=20
  From: Timothy Parez=20
  To: Christian Birkl=20
  Cc: mono-list@ximian.com=20
  Sent: Tuesday, January 27, 2004 9:09 AM
  Subject: Re: AW: [Mono-list] Assembly.LoadFrom() - =
Assembly.CreateInstance() - Activator.CreateInstanceFrom() ---> invalid =
cast


  Christian Birkl wrote:

Hi,

  Stating it is an invalid cast.
   =20
Can someone please explain me the following behaviour:

	Assuming you have Assembly "A.dll", with one class Y (no
methods, no fields, just a plain class). Now copy this assembly to
A.1.dll, A.2.dll. You now have 3 assembly, A.dll, A.1.dll, A.2.dll (they
just differ in date of last modification).

Create a new project which references "A.dll" and write the following
main function:

	static void Main(string[] args)
	{
		Type t1 =3D Assembly.LoadFrom(@"A.1.dll").GetType("A.Y");
		Type t2 =3D Assembly.LoadFrom(@"A.2.dll").GetType("A.Y");
		Type t3 =3D typeof(A.Y);		=09
		=09
		Console.WriteLine(t1.GUID);
		Console.WriteLine(t2.GUID);
		Console.WriteLine(t3.GUID);

		Console.WriteLine(t1 =3D=3D t2);
		Console.WriteLine(t1 =3D=3D t3);
		Console.WriteLine(t2 =3D=3D t3);		=09
	}

Now guess the output (MS.NET 1.1 runtime):

d899aa25-2426-3ef7-91c2-95e6f8aaed27
d899aa25-2426-3ef7-91c2-95e6f8aaed27
d899aa25-2426-3ef7-91c2-95e6f8aaed27
True
False
False

Hu? :-). Why is t1 !=3D t3, but t1 equals t2? Anyone?

So i guess that's related to your problem with your MySQL.ByteFX
DataProvider. You load your ByteFX assembly via Assembly.LoadFrom, which
references System.Data in its own "dll" scope. Your program also
references "System.Data", but somehow the "System.Data" of your program
isn't the same as in your lazy loaded ByteFX assembly.

I can't give you a solution for this, but if your application is a web
app i think you run into the problems if you're not using XSP but IIS
since IIS copies all bin\* files into some temporary directories and
there you'll get the same behaviour as stated above. (IIS does this in
order to enable dynamic reloading of web apps. Ever tried to update one
dll in your C:\inetpub\wwwroot\<App\bin\ directory? It is not locked,
you can overwrite it and if you do it, IIS will implicitly redeploy your
web app to reflect the update - makes life much easier 8-))

Christian

_______________________________________________
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list

  Hmm, what if I load System.Data.dll aswell ?


------=_NextPart_000_0089_01C3E4BF.3483DF90
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=3DContent-Type =
content=3Dtext/html;charset=3DISO-8859-1>
<META content=3D"MSHTML 6.00.2800.1276" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>I've enhanced the test code:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type t1 =
=3D=20
Assembly.LoadFrom(@"A.1.dll").GetType("A.Y");<BR>&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;=20
Type t2 =3D=20
Assembly.LoadFrom(@"A.2.dll").GetType("A.Y");<BR>&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;=20
Type t3 =3D typeof(A.Y); </FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t1.Assembly.EscapedCodeBase);<BR>&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t2.Assembly.EscapedCodeBase);<BR>&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t3.Assembly.EscapedCodeBase);</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t1.AssemblyQualifiedName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t2.AssemblyQualifiedName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t3.AssemblyQualifiedName);</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t1.GUID);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
=20
Console.WriteLine(t2.GUID);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
=20
Console.WriteLine(t3.GUID);</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t1 =3D=3D =
t2);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t1 =3D=3D =
t3);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Console.WriteLine(t2 =3D=3D t3); <BR></FONT></DIV>
<DIV><FONT size=3D2>and on .NET 1.0 it produces:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2><A=20
href=3D"file:///D:/ttt/A.1.dll">file:///D:/ttt/A.1.dll</A><BR><A=20
href=3D"file:///D:/ttt/A.1.dll">file:///D:/ttt/A.1.dll</A><BR><A=20
href=3D"file:///D:/ttt/a.DLL">file:///D:/ttt/a.DLL</A><BR>A.Y, a, =
Version=3D0.0.0.0,=20
Culture=3Dneutral, PublicKeyToken=3Dnull<BR>A.Y, a, Version=3D0.0.0.0,=20
Culture=3Dneutral, PublicKeyToken=3Dnull<BR>A.Y, a, Version=3D0.0.0.0,=20
Culture=3Dneutral,=20
PublicKeyToken=3Dnull<BR>2c21befd-5a3f-3d21-ab48-1c6c1361dc0b<BR>2c21befd=
-5a3f-3d21-ab48-1c6c1361dc0b<BR>2c21befd-5a3f-3d21-ab48-1c6c1361dc0b<BR>T=
rue<BR>False<BR>False<BR></FONT></DIV>
<DIV><FONT size=3D2>Note that LoadFrom("A.2.dll") caused A.1.dll to be =
loaded.=20
Strange.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Jarek</DIV></FONT>
<DIV><FONT size=3D2>&nbsp;</DIV></FONT>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A title=3Dtpsoftware@users.sourceforge.net=20
  href=3D"mailto:tpsoftware@users.sourceforge.net">Timothy Parez</A> =
</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A =
title=3Dwingman@techmonkeys.org=20
  href=3D"mailto:wingman@techmonkeys.org">Christian Birkl</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Cc:</B> <A =
title=3Dmono-list@ximian.com=20
  href=3D"mailto:mono-list@ximian.com">mono-list@ximian.com</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Tuesday, January 27, 2004 =
9:09=20
  AM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> Re: AW: [Mono-list]=20
  Assembly.LoadFrom() - Assembly.CreateInstance() -=20
  Activator.CreateInstanceFrom() ---&gt; invalid cast</DIV>
  <DIV><BR></DIV>Christian Birkl wrote:<BR>
  <BLOCKQUOTE cite=3Dmid000a01c3e462$28680720$0200a8c0@winghome =
type=3D"cite"><PRE wrap=3D"">Hi,

  </PRE>
    <BLOCKQUOTE type=3D"cite"><PRE wrap=3D"">Stating it is an invalid =
cast.
    </PRE></BLOCKQUOTE><PRE wrap=3D""><!---->
Can someone please explain me the following behaviour:

	Assuming you have Assembly "A.dll", with one class Y (no
methods, no fields, just a plain class). Now copy this assembly to
A.1.dll, A.2.dll. You now have 3 assembly, A.dll, A.1.dll, A.2.dll (they
just differ in date of last modification).

Create a new project which references "A.dll" and write the following
main function:

	static void Main(string[] args)
	{
		Type t1 =3D Assembly.LoadFrom(@"A.1.dll").GetType("A.Y");
		Type t2 =3D Assembly.LoadFrom(@"A.2.dll").GetType("A.Y");
		Type t3 =3D typeof(A.Y);		=09
		=09
		Console.WriteLine(t1.GUID);
		Console.WriteLine(t2.GUID);
		Console.WriteLine(t3.GUID);

		Console.WriteLine(t1 =3D=3D t2);
		Console.WriteLine(t1 =3D=3D t3);
		Console.WriteLine(t2 =3D=3D t3);		=09
	}

Now guess the output (MS.NET 1.1 runtime):

d899aa25-2426-3ef7-91c2-95e6f8aaed27
d899aa25-2426-3ef7-91c2-95e6f8aaed27
d899aa25-2426-3ef7-91c2-95e6f8aaed27
True
False
False

Hu? :-). Why is t1 !=3D t3, but t1 equals t2? Anyone?

So i guess that's related to your problem with your MySQL.ByteFX
DataProvider. You load your ByteFX assembly via Assembly.LoadFrom, which
references System.Data in its own "dll" scope. Your program also
references "System.Data", but somehow the "System.Data" of your program
isn't the same as in your lazy loaded ByteFX assembly.

I can't give you a solution for this, but if your application is a web
app i think you run into the problems if you're not using XSP but IIS
since IIS copies all bin\* files into some temporary directories and
there you'll get the same behaviour as stated above. (IIS does this in
order to enable dynamic reloading of web apps. Ever tried to update one
dll in your C:\inetpub\wwwroot\&lt;App\bin\ directory? It is not locked,
you can overwrite it and if you do it, IIS will implicitly redeploy your
web app to reflect the update - makes life much easier 8-))

Christian

_______________________________________________
Mono-list maillist  -  <A class=3Dmoz-txt-link-abbreviated =
href=3D"mailto:Mono-list@lists.ximian.com">Mono-list@lists.ximian.com</A>=

<A class=3Dmoz-txt-link-freetext =
href=3D"http://lists.ximian.com/mailman/listinfo/mono-list">http://lists.=
ximian.com/mailman/listinfo/mono-list</A>

  </PRE></BLOCKQUOTE>Hmm, what if I load System.Data.dll aswell=20
?<BR><BR></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_0089_01C3E4BF.3483DF90--