Friday, October 17, 2008

System.Reflection Interview Question

Phone screens are always fun. In programming, there are many ways to solve problems, but when asking a question, the interviewer is usually looking for a single solution. And if you don't have that answer at your fingertips, it's gotcha time. The System.Reflection namespace is a perfect example of something you need to have in your pocket for an interview. The key phrasing to look out for is a method that is highly changeable over time. We all know the difficulty in modifying production code, unit testing, QA cycles, versioning, publishing, etc, so when someone gives you a scenario that presents changeable implementation over time, you're looking for LATE BINDING, which is what System.Reflection gives you. You don't know exactly which method you're going to call at runtime, but if you've written your code correctly, you can handle any changes down the road by packaging the changing implementation in a separate assembly and binding to it at runtime when it's needed.

Let me give you an example. In your middle tier, you are going to pass a string into a private void HandleColorChoice(string myStrColor), and based on the color, you are going to implement different options. You're only starting out with red, yellow, blue, but over time, your code needs to handle even 100 different color implementations.

You could handle this scenario with a switch (I've done it myself), but if you don't have all the implementations in hand when you go live, that's not a sustainable solution. Instead, think of HandleColorChoice as your switchboard operator who decides which method to call. How does it know? Well, when does it know is the more important question...not until the string is passed, and bingo, that's not until runtime. Deciding which method to call is a piece of cake:

string colorFunctionName=string.format("Handle{0}Color", myStrColor);

We put all of these implementations in a separate assembly which you can build and deploy separately, a much easier scenario for us, and use reflection to call the methods. To use the System.Reflection namespace, you have to become familiar with the System.Type class, it will expose all the metadata for your assembly that you need to load at runtime. Here's a simple implementation for HandleColorChoice:

objAssembly=System.Reflection.Assembly.LoadFrom(str); //enter the path to your assembly
Type objType = objAssembly.GetTypes();
MemberInfo[] arrayMemberInfo;
try { //Find all static or public methods in the Object class that match the specified name.
arrayMemberInfo = objType.FindMembers(MemberTypes.Method
, BindingFlags.Public BindingFlags.Static BindingFlags.Instance
, new MemberFilter(colorFunctionName)
, "ReferenceEquals");

for(int index=0;index < arrayMemberInfo.Length ;index++)
catch (Exception e) { Console.WriteLine ("Exception : " + e.ToString() ); }

Now this is just the most basic idea of what you can do with System.Reflection, but be ready for the question! Good luck :)

No comments: