Reflection Part I

Someone asked on the Microsoft Web Dev. mailing list the other day about how they could remove their hardcoded loading of messages from their resource manager in .NET. They were doing it using some code like this-

string err=resman.GetString(“ClassNotFoundException”);

System.Reflection is one of the greatest advantages to writing managed code, and can definetly boost development productivity. For example, using reflection you could get the personal error message from the resource manager using this-

catch(Exception e) {
string err=(string)resman.GetType()
.GetMethod(“GetString”).Invoke(resman,new object[]{e.GetType().Name});
}

Providing the exception thrown was of type ‘ClassNotFound’, the above code would be effectively doing the same as the first piece of code, but without the necessity to hardcode strings.

I intend to write more stuff about the wonders of reflection in the future.

G

Gary

Gary