Sunday, June 26, 2005 - 12:42

Serialization in .NET

Serialization in .NET

As an aside, before I start talking about what I've learned about serialization in the last week, let me say that it really seems as though fate, destiny, or the religious entity of your choice seriously wants me to go to London - Saturday morning my fridge just stopped working (in fact, even worse, it started heating everything up instead of cooling it down). Coming on top of everything else, it just seems like a very distinct sign :-) And believe me, I really would love to just pack and go, but I can't do that just yet.

Anyway, on to serialization. I've kinda used it in the past, but not directly, and I was amazed to find out just how useful it can be, especially when paired with reflection.

First, two small things that are really quite obvious but I never really realized until a week or two ago. One: you don't need to implement ISerializable, you can just mark your class with [Serializable]. I'm not 100% sure why you would want to implement ISerializable; I guess maybe if you wanted to serialize it in a very specific way. If you use the Serializable attribute, all your public fields and properties will be serialized, unless you mark them as [NonSerialized]. And if you want to set any private properties manually on Deserialization, you can implement the IDeserializationCalback interface. One word of warning: classes that use the serializable attribute will return false to 'obj is ISerializable'; use obj.GetType().IsSerializable instead.

Two: saving objects to file is really, really easy. All you need to do is create a BinaryFormatter (or XMLFormatter, or whatever you want), use it to serialize the object to a stream, and then save the stream. To retrieve the object, load the stream from the file and deserialize it into the object. The only catch is that to deserialize, you need to know what kind of formatter was used to serialize the object, and use that same formatter type to deserialize it.

So those are pretty simple, and I should have figured them out ages ago. But I came across two more things, which are more complex (but not difficult to code), and are really cool.

The first one is serialization surrogates. What happens when you need to serialize an object that isn't marked as serializable and doesn't implement ISerializable, and you don't have access to the code defining the class? My first instinct was to create a wrapper class, copy all the info across, and make my wrapper class serializable. But there is an easier way: implementing ISerializationSurrogate. I don't truly understand the details of how it works (see the reference at the end for more detail), but basically what you do is create a class which implements ISerializationSurrogate and contains just two methods: SetObjectData(...) and GetObjectData(...). Each method contains two parameters, amongst others: a SerializationInfo object, and the object to serialize/deserialize. In GetObjectData() you copy the info from the object you want to serialize into the SerializationInfo object, and vice versa for deserialization using SetObjectData. The object passed to SetObjectData is initialized, in the sense that memory has been allocated for it, but no constructors have been called on it. In theory this is supposed to work fine; in practice, I found that the first thing to do is create a new object of the correct type using a constructor, otherwise you end up with null reference exceptions. Then, despite what you may see on the net and in the articles referenced below, you must return this object that you've created. I found it easiest to just use reflection to get all the public, readable, writeable, serializable properties, and iterate through those to add them to or set them from the SerializationInfo parameter.

Finally all you need to do it set it up so that your surrogate gets called instead of the normal serialization method:

IFormatter formatter = new BinaryFormatter();
SurrogateSelector ss = new SurrogateSelector();
ss.AddSurrogate(typeof(Message),
new StreamingContext(StreamingContextStates.All),
new MessageSerializationSurrogate());
formatter.SurrogateSelector = ss;

And that's it. You can do more funky stuff here if you really want to, but you can ignore the details if you prefer.

Of course, this is a recursive process - unless it's a very simple class, you'll probably find that it contains non-serializable objects itself. If it does, and if you really do need them to be serialized as well, you'll need to write serialization surrogates for them - and it can be an almost never-ending process, similar to making a deep clone. In fact, you can use serialization to make deep clones - serialize the object to a stream, then deserialize the stream into a new object, Slightly more effort than the copy constructor method of deep cloning objects, but if you have the serialization methods in place anyway, I guess you might as well use them.

Finally, the last serialization technique that I discovered this week is the ResolveEventHandler, and it is really cool. Basically, what happens if you try to deserialize an object that was serialized by a different app, and you don't have the object's type defined anywhere in your assemblies? You simply set up a ReolveEventHandler, and during the deserialization process, if the type cannot be found in any of the app assemblies or any assemblies located in the current directory, your event handler will be called, giving the type that needs to be located. You can then find the assembly (maybe by asking the user for a path), load it using Assembly.LoadFrom(), and return the loaded assembly to the calling method. The serialization process will then continue, and since the assembly is now loaded in your app domain, the next time an object of that type needs to be deserialized, the type will be defined and your event handler will not be called. It's recommended that you register your event handler just before you attempt the possibly problematic deserialization, and then deregister it immediately afterwards.

There is, of course, a lot of info on all this on the net, and on msdn. But the reference I found most useful, with really practical (almost copy & paste) examples were a set of 3 articles for MSDN Magazine. (Note that the section on SetObjectData() is out of date: the return value is no longer ignored, and the object that you've just set the data for *must* be returned).

Now, go forth and serialize!

Labels:

Tuesday, June 21, 2005 - 10:28

URL change

If you're reading this, it's probably old news to you - but the url of this blog has changed from http://occasional_solipsism.blogspot.com to http://occasional-solipsism.blogspot.com, since some ISPs seem to have problems with underscores in the domain name. I had hoped to change the url of this blog, then put another dummy blog at the old site just to redirect people to here, but blogger has apparently also realised that underscores in domain names are a bad thing, and they won't let me create a blog at the old address.

Labels:

Sunday, June 19, 2005 - 22:34

HTML issues

I thought I was almost done with an alpha version of my website - I thought I had all the tricky javascript bits done, and just had to do the mundane html stuff. Not so.

The most annoying thing is how Firefox and IE do things differently, The second most frustrating thing is that vertical settings seem to affect the horizontal view!

Firefox vs IE: in Firefox, you need to add some padding to the iframe height when you resize it, otherwise (for some unknown reason - this is another example of the horizontal-vertical interaction) you get horizontal scrollbars showing up in your iframe. In Firefox, this padding goes after the very bottom line of pixels (eg. at the bottom of the 'p' in the word 'padding'), and takes on the background colour of the page, or, if it isn't specified, the background colour of the parent page. In IE, padding gets added automatically, but forms part of the height of the iframe, so that if you have an image you get 4 pixels added to the bottom, but if you have text the 4 pixels form the bottom 4 lines of pixels - so if you subtract it, you don't get the bottom 4 pixels of the 'p' in the word 'padding'. And these 4 pixels take on the background color of the page, but if it isn't specified, the background is white instead of the background color of the parent page. And don't even ask me how it might work in other browsers .... !

Vertical vs Horizontal: this one's really weird, and seems to be IE only, since it worked fine in Firefox. Say you have a table, with size 90% of the page, with 3 columns. The size of the first two are fixed, in pixels, while the third isn't. So this 3rd column takes up the rest of the table width (assuming there's something in it: otherwise, it takes up the rest of the table width if the table has a border; if the border is set to 0, the last column just doesn't appear at all and the other columns are sized equally across the width of the table). This works fine. But, try setting the vertical alignment of the cells to "bottom" - suddenly, all three columns are sized equally across the width of the table,as if the width parameter is totally ignored! This is just totally bizarre.

So all in all, it doesn't bode well for my vision of a page that will look something like a tab-control, since I can't get the bottom of the tab images to sit flush with the top of the table.
The more I try to do anything other than totally basic HTML, the more I hate it. There are odd, unpredictable interactions, and the way different browsers interpret the html is totally non-standard. And as soon as you try to do anything vaguely dynamic in IE, you get security warnings (yes, I know you can configure the security options - but how many people browsing the web have, and how many will, or should, click on "allow the blocked content" just for your website?). And the editors all suck - they're really just a gui that inserts the html for you, rather than being designers that let you specify the look and behaviour for your page and then figure out for you how to achieve what you want. You still need to know too much about html and javascript, and the differences between browsers.

Labels:

Thursday, June 16, 2005 - 00:51

Moving Day?

There was a letter in my letter-box when I got home today- the complex that I live in has been sold (it was previously owned by just 1 person), and they are going to be selling the flats individually. This means that I have the option of buying my flat (yeah, right), or moving out.

I'm a bit shellshocked - this is just one big change too many in the last couple of months! It's not going to happen soon, I assume - they have to go through the whole formal meeeting & notification process, and I would think that they would be happy for people to carry on renting until their unit is sold - but it is going to happen, and probably within the next few months.

In a way, it's a pity that this didn't happen last month; it would probably have been a bit much, looking for a new job and a place to stay at the same time, but at least I would have known. I would almost definitely have just gone to the UK then.

Now, I dunno ... I was thinking about going to the UK in a year or so; should I think of going now instead? But I don't like to leave a job just a couple of months in ... But if I don't, I'll probably have to take another 1 year lease on a new place, and if I'm thinking of leaving it would probably be half-way or three-quarters of the way through the lease, which makes it difficult. Not to mention all the hassle of packing up my stuff, finding a new place, etc. etc. etc.... And of course my UK passport isn't ready yet; I'm still waiting for my birth certificate from home affairs (how is it that the UK can get a birth certificate to me, in another country, within two weeks - but here, it takes 2 months to get it?), and then I still have to send the application off. So I figure it would be at least 4 months before I get the passport. And one of the things that puts me off London is the accomodation issue - it seems that rent is pretty high, and most people share a flat or a house. Now I've never lived in digs, and I don't really want to start now ... I don't think I can face sharing a bathroom with a bunch of other people :-) But I doubt that I'd be able to afford a place of my own. And I'd have preferred to save a bit more before I go, just so that I don't have to stress too much about getting a job right away.

I just don't know. It's almost like a sign that I should go. Or maybe I need to look at it as a trial run, getting rid of stuff I don't need and seeing how many boxes are needed for the stuff I do need ... I dunno.

Well, it's not like I need to make a decision right now, it'll probably take a couple of months to sort out what's happening here. But still. I dunno.

Labels:

Wednesday, June 15, 2005 - 14:38

Scary court ruling

A Minnesota appeals court has ruled that the presence of encryption software on a computer may be viewed as evidence of criminal intent.

Read more about it here, and some more commentary here. Bruce Schneier just said that he was "speechless", but there are lot of comments on his blog.

This is really scary. Considering the amount of work I've done in cryptography in the last 18 months, and the amount of crypto research material and tools on my computer, I hope I'm never accused of a crime. OTOH, at least I don't use PGP.

Labels:

Sunday, June 12, 2005 - 13:35

Dell Latitude D510 - First Impressions

So my laptop finally arrived on Friday!

I've been playing around with it a bit this weekend, and while I'll do a full review later (based on Ruari's review of his D800), here are some of my first impressions:

The first thing I noticed was how it is much lighter and thinner than the Inspiron 5150 I was using for work. The power supply is also way smaller and lighter, which makes sense for something that's meant to be portable! (I'll take some photos for the full review).

As soon as I switched it on, I was amazed at how quiet it is. You almost can't tell if it's on or off, based on the sound. Even the CD drive is really quiet.

Unfortunately, the second thing I noticed when I switched it on was how the screen is much brighter at the bottom than at the top. At first it's just a bit annoying, but the longer you use it the worse it gets. The top, especially the corners, are quite dark, while the taskbar looks washed out, which makes it very difficult to set the brightness and contrast levels. It also makes it quite difficult to see highlighted items at the bottom of the screen. In fact, I suspect that it may be a flaw - TFT screens, apparently, shouldn't have this problem, although of course the viewing angle can influence the brightness quite a lot. But no viewing angle decreases the brightness gradient on my screen :-(

It runs very cool - the drive gets a bit warm after a while, but the keyboard area is totally cool.

Another annoying thing is that the touchpad is not centered, but is slightly off to the left. This is disconcerting, because when typing you tend to want to center your hands around the touchpad, which means you keep hitting the wrong keys (assuming you type using both hands, of course :-) Also, the delete key is located on the top two rows of reduced-size keys, rather than being a normal sized key down around the enter key location, which is annoying when typing but critical when trying to ctl-alt-del - you can't do it one-handed any more :-(

The basic feel is very nice - the case is slightly roughened, nice silver look (I just hope it doesn't wear off in high-usage areas). Oddly, the earphone and mic jacks are in the HDD bay, and the cd drive is in a removable module bay. So if you want to install a second battery, which goes in the module bay, you'd need to remove the cd drive. The catch to open the case is a push button, rather than the slider that I'm used to, but that's not really a big issue either way. The HDD bay doesn't sit quite flush with the case, but I plan to take it out and reseat it in the hope that that will fix it.

Maximum screen res is 1024x768, which is fine for normal stuff (internet, e-books, that type of thing) but isn't really good enough for Visual Studio coding, where you need to have lots of stuff on the screen at once (not that I've installed Beta 2 yet, but I plan to get to soon!).

Which brings us to microsoft, meaning XP. I used to really hate the way ms organised the OS - I still do, but mostly I just hate the defaults that they use. The good news is that they can generally be changed, but the bad news is that it's not always clear where to change them! For example, here's a list of things that I want to do but can't quite figure out how (so if you know, please let me know!):

  • How do I get rid of "auto updates not enabled" icon and annoying popup on login? (no, I DON'T want to enable auto updates, because I'm on dial-up. I wish they would realise that not everyone in the world has a high-speed internet link!)
  • Can I make the start menu recent programs actually show the programs that I've recently used?
  • I hate the way that ms auto populates your favourites with ms links. I've deleted them, but can I remove the favourites menu item from the folder view?
  • How do I disable autoplay completely on cd and removable drives?
  • Don't even get me started on integrated cd burning ...


I did discover that to have no user account picture on start menu, to rather have a drop down menu for restart/shutdown/etc, and to not show the welcome screen, you need to go to user accounts - change way users log on. So that's something.

Dell preinstalled windows for me, which is nice, except that I want to partition my physical drive into two partitions - one for the OS, one for data. And unbelievably, XP doesn't have any tools capable of resizing a partion! About the only tool that I've found that can do this is Partion Magic, which is pretty expensive for a one-off resize operation! Partition Expert also seems to be able to do it, and they have a demo version, but it's about 12 Mb so I can't download it from home. It's annoying, because I don't want to install everything in case I actually do have to reinstall XP just to create that new partition :-( And this is what I don't understand about microsoft - instead of building in things like MediaPlayer, and claiming that they're "integral to the operating system", why not build in actual OS tools like partition resizing? Now that's a real operating system function that is actually integral. I dunno.

I did manage to connect to the net, but it was so incredibly slow that I couldn't even log in to my gmail account. I'm hoping that it was an ISP problem and not a laptop problem, because I wouldn't even know where to start looking to fix that. I've tried it three times - once it disconnected me after about a minute of not really going anywhere, and twice it just sat there, not really going anywhere. It does just enough so that you know it is connected, but that's about it.

Overall, though, I'm pretty happy with it, except for the screen issue.

Labels:

Thursday, June 09, 2005 - 21:52

I passed!

Yay, I passed my drivers today <:-)

I still the K53 is a really dumb system, but I don't care because I passed :-) (If I'd failed, be sure you'd hear all about it. And I'm not the only one - if you're interested, do a search for K53 test on google.co.za; you'll find plenty of complaints).

Now if only my laptop would arrive ....

Labels:

Tuesday, June 07, 2005 - 22:33

Storm At Home

I've started the search for a new ISP. I looked at MWeb, but they're close to twice what I'm paying now. I looked at Polka.co.za, which turns out to also be MWeb, but cheaper - but I wasn't sure. Then I found Storm At Home, who not only claim to have some kind of compression system to speed up browsing, but - and this is the best part - they give you a month's free trial to try it out (3 months, if you pay using a mastercard). It's great, because I can try them out and see if they're better than telkom, without having to cancel my telkom contract. They have a bunch of different packages, but the one I want is R55 per month with restricted access - between 6 in the evening and 9 in the morning - since I don't need internet during the day, because I'm at work! You also get access over weekends and public holidays.

So far they seem okay - dialled up okay, although it seemed to take a long time to verify my username and password, and then said "logging on to network" for ages, although I could browse while this was going on (although I didn't realise that at first). The net seems fairly fast, although it's difficult to say for sure without doing some kind of benchmark.

I'll post a review here sometime when I've given them a more comprehensive trial, but they seem to be a reasonable alternative to telkom.

Labels:

Monday, June 06, 2005 - 22:37

Day 2

Dammit, why do I always have to dial up three times before I get connected? I think it's time to look for a new ISP ...

Today was my second day at the new job ... it was okay, but I'm still settling in. At least I've mostly got my PC set up the way I like it :-) And at least I'm past the point of being nervous walking in the door in the morning. I don't really want to blog too much about work (even though it is obviously one of the three or four big things going on with me right now and I want to talk about it!) ... it just doesn't seem like a good idea. Not that I'd be giving away company secrets or anything, but still :-) Plus, some of what I'd say would just be repeating myself - everything I said in my last post still holds true.

So what are the other big things going on? Well, sadly enough, one is getting my laptop - which Dell are now saying will arrive tomorrow, according to their online tracking system, or next Tuesday, according to their customer service rep. One is another thing that I don't really want to blog about, even though I do - suffice to say that leaving ATC has been pretty tough for a variety of reasons. And the last big thing is my driver's test on Thursday, which I don't even want to think about, never mind blog about! I know I can control the car, and in any reasonable test where I'm not so nervous I'm shaking I'd pass just fine - but I don't think that the K53 is necessarily a reasonable test, and I know I'm going to be ridiculously nervous!

In fact, I was going to join the SADev movie group on Wednesday - they're going to see Kingdom of Heaven - but since my test is at 8:45 on Thursday morning, and I'm going for a lesson first, which means I have to get up just after 6:00, I think it's probably not a good idea. Besides which, I'd just be nervous all through the movie and wouldn't enjoy it. I'll probably go watch The Wedding Date on Saturday, though (since I won't be having a driving lesson - either way! - so I'll have the afternoon free). It's one of those dumb romantic comedies that, I'm embarrassed to admit, I love watching. It's a pity life isn't like it is in the movies.

Anyway, as I said in a comment elsewhere on this blog, I've pretty much decided on atspace.com to host my website - they're free, no ads, no bandwidth limit, and they seem fairly fast and reliable. Good enough for me! And I've got all my javascript working beautifully, so now all I need to do is create some simple graphics, and write the actual content. It's a pretty simple site, but at least it'll tie up the various sites I have lying around, and give me a place to point people to to find my cv, blog, etc.

Labels:

Saturday, June 04, 2005 - 22:50

No trip in a fighter jet :-(

I didn't win the BB&D/DevDays prize of a flight in a fighter jet :(

And after stealing all my friends' entry forms too!

Oh well, I'm sure I'll get over it :-) Maybe next year ....

Labels:

Just stuff

So yesterday was my first day on the job - it was fine, the people seem nice, I spent most of the day just installing software and stuff on my machine. It sounds like it should be pretty interesting work, so that's cool ... I think that in a way I've been spoiled, working for ATC - the work was really interesting, I had friends there, the management style was excellent, and I actually enjoyed going to work every day! (Well, most days, anyway). And I don't want to end up in a job which is just a way to fill up time and earn a salary. Not that I think the new place will be like that; the guys seem genuinely enthusiastic about what they're doing, and that makes a huge difference. I guess it'll just take time to settle in and make connections with people ... making new friends is not my strong point, it tends to take me ages to feel comfortable around new people, but I'm trying :-) It'll just take some time, I guess, not just to settle in here but also to get over the old place. I'm still keeping in touch with some of the guys, but it's different when you're talking solely via e-mail and msn and never get to actually see them. It's been a pretty tough week.

Anyway, enough. Good news on the laptop front; it's been shipped and should hopefully get here on Tuesday! Which will be great ... last night it was so cold here that I couldn't decide whether to snuggle up in bed and read, or work on my website - with the laptop, I'll be able to work on my website inbed! Definitely the best of both worlds.

I haven't looked at Visual Studio 2005 Beta 2 yet ... I think I'll wait for my laptop and just install it on there.

Oh, and today's my last driving lesson! My test is on Thursday, so I'm taking the morning off work, which they've been totally cool about. Wish me luck ... those of you who've been through the K53 test will know how many little irritating things there are to remember (like ostentatiously checking each side road as you go past), so I'll need all the good wishes I can get :-)

Labels:

Thursday, June 02, 2005 - 00:58

Legal MP3 downloads?

I don't mind paying for MP3s, assuming that it's a reasonable price (especially taking currency exchange rates and bank charges into account). But I want to be able to pay using a ZA credit card (or via someline system like paypal, that, unlike paypal, does accept ZA credit cards to top up your online account). And I want to be able to not have any restrictions on the mp3 that I download - no DRM. Nothing saying I can't burn it to CD, put it on my flashdrive, copy it to my phone, play it on my PC or my laptop as often as I like. Using whatever software I like. And if there absolutely have to be restrictions, then I don't want them to change after I've bought and downloaded the MP3, like Apple did recently.

Maybe there are sites that let you do this. If so, I haven't come across them yet - I'm not talking about those russian "100% legal mp3 downloads only $0.10 per song" type sites; I want a really legal, reputable site, where I would feel comfortable paying by credit card with a reasonable level of assurance that I'm not just handing my credit card to a ... well, criminal.

There must be a huge market for this. Why aren't there tons of of sites doing this? Okay, I know, they don't own the rights for the songs. But why aren't the music producers doing this?

It's the same concept as a CD single, but at a way more affordable price. If I hear a particular song, and I want that song, why should I have to buy an entire CD? I may end up liking some of the other songs on the CD, but I may not. And even I do, they aren't what I wanted in the first place. Seems to me like it's the same kind of problem Microsoft ran into, bundling Media Player together with the OS.

Mind you, making an mp3 from a CD single might not be legal, I don't really know. I don't think it should be illegal, but then, IANAL. But even aside from that, very few songs come out as a CD single, and when they do, they cost way more than they should (say a CD contains 10 tracks; a single should then be about 1/10th the price of the CD; a bit more, because of the cost of the physical media, maybe, but in that region. And if they sold them as MP3s, there would be no physical media cost.)

It all seems a bit self-defeating, to me. Songs are played to gain publicity (via radio, TV, whatever), but then you can't buy the song easily. For instance, I've had Daniel Beningfield's "Wrap my words around you" going through my head all day, and I quite like the song, but I can't buy it because there's no way I'm going to pay R100+ for a CD containing exactly 1 song that I want.

The general consensus seems to be that the internet is forcing a paradigm shift on media sales, and that the traditional sales models just simply won't work any more and new models are needed to replace them. I just wish they'd hurry up and find a workable solution.

Labels:

Wednesday, June 01, 2005 - 01:37

Javacript's working! ... almost .....

Okay, I found the dumb errors in the script - the one was not passing an element id to the getElementByID method in the if statement, and the other one was the -1 in the loop terminator - odd, because I could have sworn that it did one iteration too many when I didn't have the -1 . Anyway, now it loads both urls, and the only problem is that it isn't triggering the iframe resize .... I'm not sure why, but I hope I can figure it out. Even manually calling the resize methods doesn't seem to work, for some reason.

I'm sure that there's probably an easier way to accomplish all this, but if there is, I don't think I want to know about it now!

Incidentally, I was going to be starting my new job tomorrow (well, I guess it's today, technically), but I've had a last minute reprieve and I only have to go in from Friday. Not often you get two days off, on your very first two days on the job :-)

Labels: