kuujinbo_dot_info

Things that make you go hmm...

Updated 2007-05.

C#/.NET, just like any other language, has it's share of things that make you scratch your head in bewilderment. So here's my least favorite list (page will be updated along the way) of things .NET.

String.Format() Escape Characters

2007-05-09

I use String.Format a lot when working with the string type. It's the closest thing C# has to Perl's variable interpolation. Yeah, a lot of people may think "variable interpolation - who cares?", but what's more readable:

string format = "page.aspx?k1={0}&k2={1}&k3={2}&k4={3}"
string page   = String.Format(format, v1, v2, v3, v4);
OR
string page = "k1=" + v1 "&" + "k2=" + v2 "&"
              "k3=" + v3 "&" + "k4=" + v4;

I'll take the String.Format() version any day. But I digress. Not so much a gotcha, because it's clearly documented, but I somteimes forget: (a) opening and closing brackets {} for everyday strings, and (b) the double quote character " for verbatim strings. In both cases the escape sequence is simply doubling-up the characters in question. With opening and closing brackets, the literal format string $(function(){});, omitting whatever you need to dump between the brackets, becomes this:

string bracket_format = "$(function(){{ content plus INDEXED PLACEHOLDERS   }});";
// literal open bracket, double here ^^  literal close bracket, double here ^^
A verbatim string, with the literal format string a literal quote ", becomes this:
string dquote_format = @"a literal quote "" ";
//         literal quote, so double here ^^

Serializing a DataSet in Binary Format

2007-04-20

Found something the other day while cleaning up some old code. Wrote an executable that serializes a DataSet using a BinaryFormatter. I had never looked in the serialized file, but for some reason I did this time. Lo and behold the file was serialized in XML - are you serious?!?! After some quick Googling, found the solution - explicitly set the DataSet's RemotingFormat property:

try {
  // forget this and you might as well not bother using
  // BinaryFormatter; file serialized as XML
  dataset.RemotingFormat = SerializationFormat.Binary;

  BinaryFormatter bf = new BinaryFormatter();
  using (Stream s = new FileStream
    ( FILENAME, FileMode.Create, FileAccess.Write, FileShare.None) )
  {
    bf.Serialize(s, dataset);
  }
  // continue processing data
}
catch { // handle exception }

I couldn't find it documented anywhere that the default serialization is XML. In fact, the BinaryFormatter documentation (albeit the example is using a Hashtable) doesn't offer any hints either. Is it just me or is requiring this extra step unintuitive and confusing? If you saw a fully-qualified class named System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, wouldn't you assume that the resultant object is serialized in binary format? Apparently not...

Running a .NET Executable from a Network Share

2007-03-08
The first time I tried I got a System.Security.SecurityException and thought?!?! A funny explanation is provided here, but everyone knows that microsoft != security. If the network share hasn't been locked-down in the first place you've got much more serious things to worry about.

Did a little research, the easy fix is:

caspol.exe -pp off -m -ag 1.2 -url "\\NETWORK_SHARE\*" FullTrust
Just make sure that the .NET directory is in your PATH, or you need to specify the full path to whatever version you're running. Full credit goes here: