C# Hash String to Hexadecimal String

Outputs a 40 character hexadecimal hash string using the MD5 algorithm.

private string ToHash(string source)
{
    byte[] bytes;
    char[] c;
    byte b;
	
    //using (SHA1 m = new System.Security.Cryptography.SHA1Managed())
    using (MD5 m = System.Security.Cryptography.MD5.Create())
    {
        bytes = m.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(source));
        c = new char[bytes.Length * 2];
        for (int i = 0; i < bytes.Length; ++i)
        {
            b = ((byte)(bytes[i] >> 4));
            // replace 0x57 with 0x37 to output uppercase
            c[i * 2] = (char)(b > 9 ? b + 0x57 : b + 0x30);
            b = ((byte)(bytes[i] & 0xF));
            // replace 0x57 with 0x37 to output uppercase
            c[i * 2 + 1] = (char)(b > 9 ? b + 0x57 : b + 0x30);
        }
    }
    return new string(c);
}

C# Thread.Sleep(TimeSpan) to Run Every 10 minutes

Based on truncating a DateTime to the second in C# while preserving the “Kind” (Local, UTC, Undefined):

dateTime = dateTime.AddTicks( - (dateTime.Ticks % TimeSpan.TicksPerSecond));

Thread.Sleep until the next 10 minute mark (e.g. Run every 00, 10, 20, 30, 40, 50 minutes):

Thread.Sleep(
    TimeSpan.FromTicks(
        (TimeSpan.TicksPerMinute * 10) - 
        (DateTime.Now.Ticks % (TimeSpan.TicksPerMinute * 10))
    )
);

Credit: http://stackoverflow.com/a/1005222/152852

In the real world, the above code appears to be waking up a fraction of a second too early. So perhaps adding an additional tick might be the way to go:

Thread.Sleep(
    TimeSpan.FromTicks(
        1 + (TimeSpan.TicksPerMinute * 10) - 
        (DateTime.Now.Ticks % (TimeSpan.TicksPerMinute * 10))
    )
);

Generic List<T> to DataTable using Reflection

The following function takes in a System.Collections.Generic.List<T> and returns a System.Data.DataTable with the properties (via reflection) of T as columns.

public static System.Data.DataTable ListToDataTable<T>(
    System.Collections.Generic.IList<T> elements)
{
    System.Reflection.PropertyInfo[] arrPropInfo = typeof(T).GetProperties();
    System.Data.DataTable dt = new DataTable();
    System.Data.DataRow dr;

    foreach (System.Reflection.PropertyInfo pInfo in arrPropInfo)
    {
        dt.Columns.Add(pInfo.Name, pInfo.PropertyType);
    }
    foreach(object elem in elements)
    {
        dr = dt.NewRow();
        foreach (System.Reflection.PropertyInfo pInfo in arrPropInfo)
        {
            dr[pInfo.Name] = pInfo.GetValue(elem, null);
        }
        dt.Rows.Add(dr);
    }

    return dt;
}

C# Serialization

Xml Serialization and Binary Serialization to a Base64 string

Xml Serialization

Snippit: Take a serializable object and serialize it into an xml string, stored in a System.Text.StringBuilder

System.Xml.Serialization.XmlSerializer serializer = 
    new System.Xml.Serialization.XmlSerializer(typeof(ObjectToSerialize));
System.Text.StringBuilder sb = new System.Text.StringBuilder();
using (System.IO.StringWriter stringWriter = new System.IO.StringWriter(sb))
{
    serializer.Serialize(stringWriter,  myObject);
    stringWriter.Close();
}

Binary Serialization to Base64 string

Snippit: Take a serializable object and serialize it into a Base64 string

byte[] bResult;
System.Runtime.Serialization.IFormatter formatter =
    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (System.IO.MemoryStream s = new System.IO.MemoryStream())
{
    formatter.Serialize(s, myObject);
    bResult = s.ToArray();
    s.Close();
}
string s = Convert.ToBase64String(bResult);

Snippit: Deserialize

ObjectToSerialize myObject = null;
byte[] b = Convert.FromBase64String(str);
System.Runtime.Serialization.IFormatter formatter =
    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (System.IO.MemoryStream ms = System.IO.new MemoryStream(b))
{
    myObject = (ObjectToSerialize)formatter.Deserialize(ms);
    ms.Close();
}