Thursday, April 28, 2005

Sometimes performance means nothing.

The code pretty much says it all here, so let's just dig in m'kay?

namespace Eronel.Common {
public class Utility {
Utility() {}

public static void ExecuteStaticArgsForConsole(Type type, string[] args) {
string option = Regex.Replace(args[0], "^[/-]", String.Empty);
try {
type.GetMethod(option,
BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase).Invoke(null, null);
} catch {
Console.WriteLine("Option not found.");

MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static);

if(methods.Length > 0) {
Console.Write("Valid options are:");
for(int i = 0; i < methods.Length; i++)
if(i == 0) {
Console.Write(" {0}", methods[i].Name);
} else if(i == methods.Length - 1) {
Console.WriteLine(" and {0}.", methods[i].Name);
} else {
Console.Write(", {0}", methods[i].Name);
}
}
}
}
}
}
}


And you've got a simple little ConsoleApp like so:

using Eronel.Common;

namespace StaticInvocation {

public class Program {
[STAThread]
static void Main(string[] args) {
if(args.Length > 0) {
Utility.ExecuteStaticArgsForConsole(typeof(Program), args);
} else {
Program program = new Program();
program.Run();
}
Console.ReadLine();
}

public static void Howl() {
Console.WriteLine("ARRROOOOOOOH!");
}

public static void Bark() {
Console.WriteLine("WOOF!");
}

public static void Whine() {
Console.WriteLine("No kitty that's mah pot pie!");
}

void Run() {
Console.WriteLine("Program Started...");
}
}
}


Of course you could do more, this is just an example. It works, it makes all those switch statements a thing of the past, and it's easy on the eyes. What more could you want?
Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?