January 31, 2009

Pinning Arrays

Example of pinning an array While pinned the array stays locked in memory at its current location
 byte[] bytes = new byte[100];
 bytes[0] = 1;
 bytes[1] = 2;
 bytes[2] = 3;
 bytes[3] = 4;

fixed (byte* pBytes = &bytes[0]) 
{
...
}

January 18, 2009

Using Reflection To Get Method Name

Output test class and method name:
string classAndMethodName =
  System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString() +
  "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString());

Useful Byte Array Debugging Classes

Some useful debugging classes for debugging byte arrays. Useful for unit tests.
internal static class ByteArrayHelper
{
 // Output the byte array as a C# style byte array variable declaration 
 // where the variable name of the byte array is passed as a parameter
 public static string ToCSharpByteArrayDeclrn(string name, byte[] bytes)
 {
  StringBuilder res = new StringBuilder(bytes.Length*6 + 64 + name.Length);
  res.Append("byte[] " + name + " = new byte[" +
                      bytes.Length.ToString(CultureInfo.InvariantCulture) + "] { ");
  int ix = 0;
  for (; ix < bytes.Length - 1; ix++)
  {
   res.Append("0x" + bytes[ix].ToString("X2",
                                      CultureInfo.InvariantCulture) + ", ");
  }
  for (; ix < bytes.Length; ix++)
  {
   res.Append("0x" + bytes[ix].ToString("X2",
                                      CultureInfo.InvariantCulture));
  }
  res.Append(" };");
  return res.ToString();
 }

 // Checks whether 2 byte arrays are exactly the same, ie. they have 
 // the same length and the same values at each array entry
 public static bool AreTheSame(byte[] bytes1, byte[] bytes2)
 {
  bool areTheSame = (bytes1.Length == bytes2.Length);
  for (int ix = 0; (ix < bytes1.Length) && areTheSame; ix++)
  {
   areTheSame = bytes1[ix] == bytes2[ix];
  }
  return areTheSame;
 }
}

January 1, 2009

WPF Dispatcher

See here for an example
// Similar to Winforms Control.BeginInvoke
Dispatcher.BeginInvoke(DispatcherPriority.Background,
  (SendOrPostCallback)delegate 
  {  
    Progress.SetValue(ProgressBar.ValueProperty, progress); 
  }, 
null);

Converting a Winform Icon to a WPF Icon

private static ImageSource WinFormIconToWpfIcon(System.Drawing.Icon icon)
{
    System.IO.MemoryStream iconStream = new System.IO.MemoryStream();
    icon.Save(iconStream);
    iconStream.Seek(0, System.IO.SeekOrigin.Begin);
    IconBitmapDecoder ibd = new IconBitmapDecoder(iconStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
    return ibd.Frames[0];
}

Access Settings/Resource in c#

Settings take the following form:
// eg. When the setting is a string
$AppNamespace$.Properties.Settings.Default.$PropertyName$ = "somechange"; 
$AppNamespace$.Properties.Settings.Default.Save();
OR
// see the Settings.Designer.cs file for the namespace
using $SomeNamespace$.Properties$
...
// eg. When the setting is a string
Settings.Default.$PropertyName$ = "somechange"; 
Settings.Default.Save();

// to use
myTextBox.Text = Settings.Default.$PropertyName$
Can only save "User Settings" and they do not get saved to the local config file but to: $Drive$:\Documents and Settings\$UserAccount$\Local Settings\Application Data\$ApplicationName$\...

To Access a Resource from a ".resx" file:

var $VariableName$ = Properties.Resources.$ResourceName$