Useful generic example

The following is a great example of the use of generics to provide strongly typed serialization:

using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace SerilizationTest
{
    static class Pickler
    {
        public static T UnPickle<T>(Stream s)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            return (T)xs.Deserialize(s);
        }

        public static void Pickle<T>(Stream s, T o)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            xs.Serialize(s, o);
        }

        public static string Pickle<T>(T o)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            StringWriter sr = new StringWriter();
            xs.Serialize(sr, o);
            return sr.ToString();
        }

        public static T UnPickle<T>(string s)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            StringReader sr = new StringReader(s);
            return (T)xs.Deserialize(sr);
        }

        public T UnPickle<T>(XmlNode node)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            StringReader sr = new StringReader(node.OuterXml);
            XmlReader xr = XmlReader.Create(sr);
            return (T)xs.Deserialize(xr);        
        }
    }
}

Minor Namespace Feature

Namespaces in C# and packages in Java are a convenient method of keeping related classes together. If you want to use code in another package in java you need to import the classes. This means that two distinct namespaces are entirely unrelated.

I used to think that the C# namespaces worked in the same way.  However it seems that namespaces form a hiearchy.

Namespace A.B.C automatically has access to Namespaces A and A.B

This is a minor detail but is not that clearly stated in the tutorials that you normally get.  It is in the language guide but not as clearly stated as it could be. 

Embedding a screen saver in a .net container control.

I have been experimenting with a simple means of embedding a screensaver into a control:

private void button1_Click(object sender, EventArgs e)
{
EmbedScr(@”c:windowssystem32ssmyst.scr”, panel1.Handle);
}
private void EmbedScr(string filename, IntPtr Handle)
{
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = “cmd.exe”;
psi.Arguments = string.Format(@”/c “”{0} /p {1}”””, filename, panel1.Handle);
psi.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(psi);
}

This allows for dynamic decoration of controls allowing a ui to become very different very quickly.