Navigation
Links

Powered by Squarespace
Monday
Sep062004

IPAddress class serializable?

Back in 2002 I posted a question in Usenet about the IPAddress class being serializable, but not serializing in ASP.NET. I just found the post, and noticed that no one responded with the reason why. So, here it is:

There are two types of serialization in .NET: System.Runtime.Serialization and System.Xml.Serialization. Runtime serialization uses the serializable attribute while XML serialization does not. Since ASP.NET uses XML serialization, the attribute was irrelevant.

The error I received was:

System.Net.IPAddress cannot be serialized because it does not have a default public constructor

One of the requirements of the XmlSerializer is that the class it is trying to serialize must have a default public constructor.

Thursday
Aug262004

Is it reference or value equality?

Probably reference. Checking for value equality is unintuitive - by default == is a reference equality check.
object a = 1;
object b = 1;
Assert.IsFalse (a == b);

Object a doesn't equal object b since boxing a as object created once instance, and boxing b created another. Since == is a "reference type equality operator", it will only compare the references.

Using any of these methods works fine:
Assert.IsTrue (a.Equals (b));
Assert.IsTrue (((int)a).Equals (b));
Assert.IsTrue (Object.Equals (a, b));
Assert.IsTrue ((int)a == (int)b);


When passing objects around, the simple solution is to use the Equals() method as it may perform value equality if the object overrides Equals(). So, value equality is somewhat easy to accomplish for individual objects, but you run into this problem again with collections:
Assert.IsFalse (
    new string[] { "abc", "def" }.Equals (
    new string[] { "abc", "def" }));
Assert.IsFalse (new int[] { 1, 2, 3 }.Equals (new int[] { 1, 2, 3 }));
Assert.IsFalse (
    new object[] { 4, "stuff", DateTime.Parse ("1/1/2004 1:00 AM") }.Equals (
    new object[] { 4, "stuff", DateTime.Parse ("January 1, 2004 01:00") }));


There is only reference equality with ArrayList since Object.Equals() is inherited not overridden.
Assert.IsFalse (
    new System.Collections.ArrayList (new int[] { 1, 2, 3 }) == 
    new System.Collections.ArrayList (new int[] { 1, 2, 3 }));
Assert.IsFalse (
    new System.Collections.ArrayList (new int[] { 1, 2, 3 }).Equals (
    new System.Collections.ArrayList (new int[] { 1, 2, 3 })));


Here's my solution to perform value equality for collections. And now:
Assert.IsTrue (Collection.Equals (
    new System.Collections.ArrayList (new int[] { 1, 2, 3 }), 
    new System.Collections.ArrayList (new int[] { 1, 2, 3 }), true));
Assert.IsTrue (Collection.Equals (
    new int[] { 1, 2, 3 }, new System.Collections.ArrayList (
    new int[] { 1, 2, 3 }), true));
Assert.IsTrue (Collection.Equals (
    new object[] { 1, 2, 3 }, 
    new int[] { 1, 2, 3 }), true);
Assert.IsTrue (Collection.Equals (
    new object[] { 4, "stuff", DateTime.Parse ("1/1/2004 1:00 AM") }, 
    new object[] { 4, "stuff", DateTime.Parse ("January 1, 2004 01:00") }), true);


Here's a real world example:
/// <summary>Always accepts the public key hardcoded in the byte array as a valid certificate.</summary>
public sealed class AcceptTrustedCertificate : ICertificatePolicy {
    public bool CheckValidationResult (
        ServicePoint srvPoint,
        X509Certificate certificate,
        WebRequest request,
        int certificateProblem) {
        //TODO: verify certificateProblem is an acceptable value
 
        // return true if certificate returned is equal to the certificate's public key
        return Collection.Equals (
            certificate.GetPublicKey (),
            new byte[] { 48, 130, 1, 10, 2, 130, 1, 1, 0, 206, 214, 212 }, true);
    }
}
Thursday
Jul152004

XML/object libraries are deprecated

If you take any one of the many RSS libraries, including my own RSS.NET, ATOM libraries, or even OPML libraries, you find mostly the same functionality. Each read and write to a specific XML format. Some take advantage of the XML libraries provided by the environment, and some even read and write through streams. But they all solve the same problem - reading and writing objects or structures to and from an XML file, abstracting away XML from the client.

From my experience with RSS.NET, I believe that this code is useless. These formats will eventually evolve and new formats will arise. These libraries should evolve to solve a different problem: generating libraries that read and write specific XML formats.

Armed with a XML schema and an object mapping XML file, this new type of library should be able to generate libraries themselves whose purpose is to read and write to a specific XML file format. After this assembly is produced, developers can use this "strongly typed" object and its formatReader and formatWriter objects to read and write documents. Since this library would use code generation, developers would code this library against the XML schema standard, resulting in generic code that could be applied to any XML format.

Reusable object library generation

The result could be RSS, ATOM, and OPML objects who can read and write their respective formats a piece at a time (by using a stream) or, all at once. They would be schema compliant, optimized for each specific version, and most importantly, extremely easy to use by the end developer.
Friday
Jul092004

This weblog is preliminary and is subject to change

The services that George provides to you are subject to the following Terms of Use (TOU). George reserves the right to update the TOU at any time without notice to you. The most current version of the TOU can be reviewed here.

Through his network of websites, George provides you with access to a variety of resources, including source code, download areas, communication forms, and product information (collectively "Content"). This Content, including any updates, enhancements, new features, and/or the addition of new Content, as subject to the TOU.

This website supports a preliminary release of a weblog that may be changed substantially prior to final public release. This website is provided for informational purposes only and George makes no warranties, either express or implied, in this website. Information in this website, including all postings, URL and other Internet Web site references, is subject to change without notice. The entire risk of the use or the results from the use of this website remains with the user. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this website may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of George.

George may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this website. Except as expressly provided in any written license agreement from George, the furnishing of this website does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

The opinions expressed herein do not represent George's personal opinions or his employer's, family, or friends' view in any way.

Source code

Source code (software) that is available on this website is licensed under this license:

Copyright © 2003-2010, George Tsiokos. All Rights Reserved.

Microsoft Reciprocal License (Ms-RL)

This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.

1. Definitions

The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.

A "contribution" is the original software, or any additions or changes to the software.

A "contributor" is any person that distributes its contribution under this license.

"Licensed patents" are a contributor's patent claims that read directly on its contribution.

2. Grant of Rights

(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.

(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.

3. Conditions and Limitations

(A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. You may license other files that are entirely your own work and do not contain code from the software under any terms you choose.

(B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.

(C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.

(D) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.

(E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.

(F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
Page 1 ... 6 7 8 9 10