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());
Now in Visual C# compiler versions 6, to get the class name we can use:
  string className = nameof(someClass);
and to get the method name
...
public static string GetCurrentMethodName([System.Runtime.CompilerServices.CallerMemberName] string name = "")
{
    return name;
}
...
  string methodName = GetCurrentMethodName()

Useful Byte Array Debugging Classes

Some useful debugging classes for debugging byte arrays. Useful for unit tests.
public 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(
      this byte[] bytes, string name)
  {
    string res = "byte[] " + name + " = new byte["
        + bytes.Length.ToString() + "] { ";
    int ix = 0;
    for (; ix < bytes.Length - 1; ix++)
    {
      res += "0x" + bytes[ix].ToString("X2") + ", ";
    }
    for (; ix < bytes.Length; ix++)
    {
      res += "0x" + bytes[ix].ToString("X2");
    }
    res += " };";
    return res;
  }

  // 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(this 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;
  }

  // XOR together 2 byte arrays
  public static byte[] XOr(this byte[] bytes, byte[] xorBytes)
  {
    Debug.Assert(bytes.Length == xorBytes.Length,
      $"Byte arrays are different sizes: first is {bytes.Length} " +
      $"and the second is {xorBytes.Length}");
    byte[] res = new byte[bytes.Length];
    int len = xorBytes.Length;
    for (int ix = 0; ix < bytes.Length; ix++)
    {
      res[ix] = (byte)(bytes[ix] ^ xorBytes[ix % len]);
    }
    return res;
  }
}

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$