Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / October 2004

Tip: Looking for answers? Try searching our database.

XmlSerializer initiated with given type returns object of different type (other assembly)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Philipp Sumi - 23 Oct 2004 11:53 GMT
Hello Newsgroup

I have a strange situation with a VS.net Add-In that uses the
XmlSerializer class for deserialization. As the problem seems to be the
same for the Add-In as for designers, I thought I might be lucky here...

In the code below, I pass a given type to the serializer class. However,
the type of the object that is returned by the serializer is a different
one so I cannot cast it.

//get the type -> same problem with typeof(...)
MappedObjectNode node = new MappedObjectNode();
Type type = node.GetType();

//store assembly location
string loc1 = type.Assembly.Location;

XmlSerializer serializer = new XmlSerializer(type);
using (Stream stream = file.OpenRead())
{
  //perform deserialization
  object obj = serializer.Deserialize(stream);

  //get assembly location
  string loc2 = obj.GetType().Assembly.Location;
}

When running this code, the strings loc1 and loc2 are different. VS.net
creates some kind of shadow copies for the add-in. However, the
serializer and the add-in use different assemblies:

loc1 (object created in add-in):
c:\dokumente und einstellungen\sumi\
lokale einstellungen\anwendungsdaten\assembly\
dl2\2dao39nh.tnl\4ehanxn5.m8y\
3bed8731\d814a9c3_828fc401\dao.net.dll

loc2 (object created by serializer):
c:\dokumente und einstellungen\sumi\
anwendungsdaten\microsoft\visualstudio\7.1\
projectassemblies\qcwp2ofq01\dao.net.dll

Strange enough, if I don't touch my solution and immediately open the
add-in, the copy within the "visualstudio\7.1"-folder is not being used
(loc1 and loc2 are the same as loc1 above) and my cast succeeds. This
works fine for me, but the add-in is part of an open-source project
which will hopefully be used by many people...

Right now, I'm stuck - any suggestion is highly appreciated

Thanks for your advice

Philipp
Frank Hileman - 24 Oct 2004 00:28 GMT
Hello Philipp,

You are correct that Visual Studio uses shadow copies. We found this problem
with custom designers, not with add-ins. But perhaps the same problem exists
for add-ins.

Usually when you have a type mismatch VS is loading one via LoadFrom and one
via the normal mechanism. In this case the CLR thinks they are two different
dlls and gives a different identity to the same type from each. I am not
sure what is going on in your situation, but the best solution we found is
to make sure problematic dlls are strongly named and stored in the GAC. We
do not do this with our add-ins, but we do this with our designer dlls.

If your tool uses reflection then you may have even more problems. If you
can redesign to avoid that reflection based serializer, and instead do your
own custom serialization to xml, you may have better luck.

Best of luck,
Frank Hileman

check out VG.net: www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor

> Hello Newsgroup
>
[quoted text clipped - 49 lines]
>
> Philipp
Philipp Sumi - 24 Oct 2004 20:30 GMT
Hi Frank

Thanks for your reply. The GAC solution works very well, thanks for this
one :-)

However, i came up with a simpler solution that works for now - as my
add-in only needs a very small subset of the referenced library, I added
a link to the needed classes to my add-in project and compiled them
right into the add-in assembly. This works very well and I don't have to
install anything into the GAC by now. Of course, this solution doesn't
scale very well - if the add-in gets more complex, I will have to fall
back to the GAC...

Best wishes

Philipp

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.