Personal Inertia

A collection of learnings and opinions.

Tuesday, February 10, 2009

Listing the properties of an object

Sometimes you just want to print out the state of some reasonably rich object (for logging or GUI mock-up or whatever). This code gives you a PropertyGetter class with two easy-to-use static methods. listProperties returns a name: value string listing of the properties on the input object, for easy logging.

If you want the object's properties as a list of properties you can get that as well.

NB: This simple class does not recurse through complex objects, so you will only ever get the top-level of a property.

Code:

using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Code_Examples {
class PropertyGetter {
public static string listProperties(object someObject) {
StringBuilder sb = new StringBuilder();

List<pair> props = getProperties(someObject);

foreach (Pair prop in props) {
sb.AppendLine(prop.Name + ": " + prop.Value);
}
return sb.ToString();
}

public static List<pair> getProperties(object someObject) {
List<pair> dic = new List<pair>();

PropertyInfo[] props = someObject.GetType().GetProperties();
foreach (PropertyInfo prop in props) {
Pair t = new Pair {
Name = prop.Name,
Value = prop.GetValue(someObject, null)
};
dic.Add(t);
}
return dic;
}
}

public class Pair {
public string Name {
get;
set;
}
public object Value {
get;
set;
}
}
}

Monday, January 26, 2009

Logging snippets ready to go

My last post was some simple intro to how you can make your own snippets - here are some easy ones I made to make logging with log4net easier.

This first one is one I use simply to get a logger with the name of the current class. I put this in most every class. Nothing fancy here, just type log and press tab to get a logger named log.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Logging
</Title>
<Author>Tomas Ekeli</Author>
<Description>Initializes a logger with the name of the current class</Description>
<HelpUrl />
<SnippetTypes />
<Keywords />
<Shortcut>log</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>log4net.dll</Assembly>
<Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>log4net</Namespace>
</Import>
</Imports>
<Declarations />
<Code Language="CSharp" Kind="" Delimiter="$"><![CDATA[protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); ]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


Now you've got a logger up and running you'll be doing a lot of calls to log.debug("somestring") and even more with formatting on that string. So here's a snippet for that:


<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>logFormattedString</Title>
<Author>Tomas Ekeli</Author>
<Description>log formatted string</Description>
<HelpUrl></HelpUrl>
<SnippetTypes />
<Keywords />
<Shortcut>lsf</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>log4net</Assembly>
<Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>log4net</Namespace>
</Import>
</Imports>
<Declarations>
<Literal Editable="true">
<ID>Level</ID>
<Type />
<ToolTip>Log level</ToolTip>
<Default>Debug</Default>
<Function />
</Literal>
<Literal Editable="true">
<ID>Message</ID>
<Type />
<ToolTip />
<Default>field value: {0}</Default>
<Function />
</Literal>
<div class="youtube-video"><object Editable="true">
<ID>Field</ID>
<Type>Object</Type>
<ToolTip>The field to log</ToolTip>
<Default></Default>
<Function />
</object></div>
</Declarations>
<Code Language="csharp" Kind="method body" Delimiter="$"><![CDATA[log.$Level$(string.Format("$Message$", $Field$));]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


Finally a very nice thing about logging is when you can push the context on a stack for inclusion. I use this all the time. This snippet can be used as a surrounding snippet as well (mark the text you want to surround in a context and right-click -> Surround-with


<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>logFormattedString</Title>
<Author>Tomas Ekeli</Author>
<Description>log formatted string</Description>
<HelpUrl></HelpUrl>
<SnippetTypes />
<Keywords />
<Shortcut>lsf</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>log4net</Assembly>
<Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>log4net</Namespace>
</Import>
</Imports>
<Declarations>
<Literal Editable="true">
<ID>Level</ID>
<Type />
<ToolTip>Log level</ToolTip>
<Default>Debug</Default>
<Function />
</Literal>
<Literal Editable="true">
<ID>Message</ID>
<Type />
<ToolTip />
<Default>field value: {0}</Default>
<Function />
</Literal>
<div class="youtube-video"><object Editable="true">
<ID>Field</ID>
<Type>Object</Type>
<ToolTip>The field to log</ToolTip>
<Default></Default>
<Function />
</object></div>
</Declarations>
<Code Language="csharp" Kind="method body" Delimiter="$"><![CDATA[log.$Level$(string.Format("$Message$", $Field$));]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

Code snippets

I like anything which makes my day easier. Code snippets are one of these things. You can make your own code-snippets by editing xml directly, using the code snippet schema reference, or you could use a tool. Snippet Editor is just such a tool. With it making and editing snippets is dead simple!

One thing I've noticed with it, though, is that you cannot make a "SurroundsWith" snippet in this tool. To do this you have to edit the xml yourself. It's easy when you know how - just add the following to the header section of your snippet definition:


<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>


And in your snippet section you add selected and end fields where you would insert the code you want to surround with the snippet (the following example would surround the selected code with curly braces).


<Code Language="csharp" Kind="method body" Delimiter="$">
<![CDATA[{ $selected$ $end$}]]>
</Code>

Tuesday, October 14, 2008

Developing ASP pages with Firefox

I prefer Firefox 3 to IE7 when developing (understatement of the week), particularly because it has Firebug and YSlow (an add-on for an add-on, would that be a quasi-add-on?). IE7 has the developer toolbar, but it's not as good (in my experience).
Naturally I have Firefox as my default browser, so when I launch ASP pages in the development server from Visual Studio they open in Firefox. And it is soooo slooooow. It's ridiculous, it's like being on my old 28.8 modem back in the mid-90s.
How can this be? Firefox isn't slower than IE7. That cannot be, no benchmarks point in that direction (OK, those are just javascript, but you get the gist of it).
It turns out the problem is with Firefox 3 and the development server (Cassini). I haven't dug deeply into it, just observed the problem, found the fix and applied it.
I've no time to check it right now, but disabling IPv6 seems a bit ham-handed. It should be possible to just include localhost in the network.dns.ipv4OnlyDomains preference. This is a string containing a comma-separated list of domains.

Technorati Tags: , , , , , , , ,

Tuesday, October 7, 2008

C# Compiler Could Not Be Created

I was having memory-problems on my dev machine (2 Gigs don't go far these days), and after a few resets I was getting an exception whenever I tried to open a solution containing a C# project in VS2005. It told me the C# compiler could not be loaded, and I should reinstall VS.
Eh? No way. That's an entire day, getting it back to where I want it.
So I open the Help -> About to see if I can get some info about what's wrong. There I'm hit by a Package Load Failure, coming from ReSharper (we loves it). This looks like something.
A quick google for this, and I find Aaron Stebner has a possible solution. The tool he references in that post didn't find any problems, but running through the steps to re-build the native libraries did.
So these are the steps in case that blog ever disappears:
  1. Open a cmd prompt
  2. Run del %windir%\Assembly\NativeImages_v2.0.XXXXX_32\Microsoft.VisualStu#
    (where XXXXX is the build number of the .NET Framework 2.0 that
    you have installed - you can figure that out by looking at the name of
    the folder at %windir%\Microsoft.NET\Framework\v2.0.XXXXX)
  3. Run %windir%\Microsoft.NET\Framework\v2.0.XXXXX\ngen.exe update /queue
That's done it. A restart of VS and we're up and running again. Now I just have to convince my boss I should get 2 more gigs of RAM.

Wednesday, October 1, 2008

Personal Information Numbers

A quick set of Regexes to check for Personal Information Numbers from the nordic countries.
I use these to check the format only, no fancy calculation of checksums etc.

  • Norwegian: DDMMYYXXXXX
    \b[0-4]\d[014]\d{3}\d{5} 

  • Swedish: YYMMDDCXXXX (C == '-' || '+')
    \b\d{2}[01]\d[0-3]\d[-\+]\d{4}

  • Danish: DDMMYY-XXXX
    \b[0-3]\d[01]\d{3}-\d{4}

  • Finnish: DDMMYYCZZZQ (C == '-'||'+'||'A' ; Q == [01234536789ABCDEFHJKLMNPRSTUVWXY])
    \b[0-3]\d[01]\d{3}[-\+A]\d{3}[0-9A-FHJ-NPR-Y]

  • Icelandic: DDMMYY-XXXC (C == 0 || 9)
    \b[0-3]\d[01]\d{3}-\d{3}[09]

Tuesday, September 30, 2008

Podcasts

I listen to a lot of podcasts. What can I say, I bore easily. When I have to do something that's not social and takes more than a few minutes I often pop the earbuds in and start listening. My player, the Zen V 2Gb, is far too small to contain any music, and podcasts are a great way of getting some intelectual topping in the daily drudgery.

I thought I'd post the list of podcasts I enjoy regularly:
  • CBC Radio Quirks and Quarks
    A great show about the stories breaking in science. Definitely a Canadian slant, and they've been running theme shows about Canada's carbon-footprint and such lately (which doesn't interest me that much), but still one of the greatest.

  • FLOSS Weekly
    This show died for a while, but they're back with a vengeance. In-depth interviews with important people in the open-source world. Could it be better?

  • In Our Time With Melvyn Bragg
    Melvyn is a delightfully pretentious British chap interviewing academics on interesting themes. Always interesting, and a really eclectic selection of themes.

  • IT Conversations
    One of the really early podcasts, this is excellent quality. I listen to the entire feed, which I think syndicates a lot of other feeds. Great content from biomedical/genetics to philosophy and CTO-grade stuff. There's always something here you haven't heard about.

  • Math Mutation
    This is a new one for me. Erik Seligman talks very briefly (3-5 minutes) about some interesting topics from the world of Math and Numbers. Good content!

  • StarStuff with Stuart Gary
    I'm a sucker for the starry nights, and this recent discovery seems right up my alley. I've only listened to two yet, but I think it's a keeper!

  • The Java Posse
    My favourite! The posse have weekly commentaries on the tech-world with a heavy java-slant. All the members are world-class developers with strong opinions and the arguments to back them up. It makes my day every time I see a new one pop into my playlist.

  • The Skeptic's Guide to the Universe
    Tackling the idiocy and numb-thinking out there head on, the Skeptical Rogues are not afraid to delve deeply into any theme they find. It's so refreshing to see that there are intelligent people out there, not just nuts.

  • This Week in Science - The Kickass Science Podcast
    Another recent discovery on my part. I'm not sure if this is a keeper yet. The themes seem interesting, but they're just not catching me yet. I'll keep them around for a few weeks, and see if they're worth it.

  • This Week in Tech
    I've a feeling this is the big one. Leo Laporte is a great host, and he gets a lot of interesting punters. Whenever they talk about something I know they always manage to get it dead-wrong though, so I take their opinions with a grain of salt. Still, the show is a gem.
Definitely a Tech/Science slant, eh? Any others I should know about, or comments are much appreciated :)