Click here to Skip to main content
15,893,337 members
Articles / Programming Languages / C#

ListView Personalization

Rate me:
Please Sign up or sign in to vote.
4.20/5 (6 votes)
30 Jul 2007CPOL1 min read 49.1K   1.8K   43  
An article on the personalization and persistance of ListView settings
using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;

namespace Personalisation
{
	/// <summary>
	/// Class that resolves deserialisation problems.
	/// Also useful to make sure most recent version of the ColumDetails is used even if the serialised version was stored against an old version.
	/// If the ColumnDetails structure changes then this can be used to switch versions.
	/// </summary>
	internal sealed class ColumnDetailsDeserialisationBinder : SerializationBinder 
	{
		public override Type BindToType(string assemblyName, string typeName) 
		{
			// For each assemblyName/typeName that you want to deserialize to
			// a different type, set typeToDeserialize to the desired type.
			String assemblyVersion = Assembly.GetExecutingAssembly().FullName;
			String columnDetaisTypeName = typeof(ColumnDetails).FullName;

			if (assemblyName != assemblyVersion && typeName == columnDetaisTypeName) 
			{
				// To use a type from a different assembly version, 
				// change the version number.
				// To do this, uncomment the following line of code.
				assemblyName = assemblyVersion; //assemblyName.Replace("1.2.0.0", "2.0.0.0");

				// To use a different type from the same assembly, 
				// change the type name.
				//typeName = "Version2Type";
			}

			// The following line of code returns the type.
			Type typeToDeserialise = Type.GetType(String.Format(CultureInfo.InvariantCulture, "{0}, {1}", 	typeName, assemblyName));
			return typeToDeserialise;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
n.runs
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions