Hello World - Reflection Style
My name is Kathy Kam and I am the newest addition to the Common Language Runtime (CLR) Program Management (PM) team. Like another PM on my team, JoelPob, I also grew up in the "Land Down Under". I left Sydney to pursue a degree in Computer Engineering and Mathematics at the University of Michigan - Ann Arbor. Upon graduation, I joined Microsoft as a developer for Microsoft Office Outlook. Four years later, after shipping Microsoft Office System 2003, a handful of Service Packs and working on Office 12 for two years, I decided to become a PM on the CLR team and here I am, writing my first post!
This blog will be a record of my insights to the .NET world. In the computing community, the first thing any developer writes is a "Hello World" program. Since this is a blog for all the computer geeks in us. Here it is, "Hello World" Reflection style:
using System;
using System.Reflection;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
foreach (FieldInfo fi in typeof(HelloObj).GetFields())
Console.Write(fi.Name + " ");
}
}
class HelloObj
{
// My output
public string Hello;
public int World;
public bool from;
public int[] Kathy;
public float Kam;
}
}
The output will be:
> Hello World from Kathy Kam
Comments
- Anonymous
September 27, 2005
lol. That is one of the cleverest ways I've seen to do "Hello World". - Anonymous
September 27, 2005
Hi!
Just to be the wiseguy that I am, I have to point out that your code is broken. It depends on Type.GetFields() to return the fields in a particular order, but the order is not guaranteed.
Say hi to Joel for me :-) - Anonymous
September 27, 2005
Hi Kathy,
The geek world welcomes you.
BTW, do you really need the public fields ? And the lower case in "from".. hum.. And "HelloObj" ? It is an object already ? :) The FxCop team will ban this hello, no ?
Just kidding :) - Anonymous
September 27, 2005
Nice way to do a hello world Kathy,All the best at the CLR team - Anonymous
September 27, 2005
Hello! =)
Happy to see that you've started a blog, another one for my list and more information to gather :)
Welcome!
Marlun, Sweden - Anonymous
September 27, 2005
Hi Hugo,
Yeah, I was not following the Design Guidelines. My bad. I made "from" lower case so that it spells it out like a sentence correctly. Regarding "HelloObj"... old habits die hard.
Cheers,
Kathy - Anonymous
September 27, 2005
Hi Kathy,
you're welcome. The first question that came to my mind when I was looking at your program is why 'Kathy' field is an array? Is there any special meaning in this?
Sorry if I'm missing something obvious :)
Regards,
Dmitry - Anonymous
September 29, 2005
Kathy, aren't you heared about new Lambda-style programming? Here is more modern way to do things ;-)
static void Main(string[] args)
{
List<FieldInfo> fields = new List<FieldInfo>(typeof(HelloObj).GetFields());
List<string> fieldNames = fields.ConvertAll<string>(
delegate(FieldInfo f) { return f.Name+" "; });
fieldNames.ForEach(Console.Write);
} - Anonymous
September 29, 2005
Or even shorter:
static void Main(string[] args)
{
new List<FieldInfo>(typeof(HelloObj).GetFields())
.ConvertAll<string>(delegate(FieldInfo f) { return f.Name+" "; })
.ForEach(Console.Write);
} - Anonymous
October 15, 2005
Hi Kathy,
Congrats on your new position. That was certainly a creative way to Hello the world.
By the way, since you just joined the team, the interview experience must be fresh in your mind. Can you blog about your PM interview experience in the CLR team?
Thanks
SN - Anonymous
February 13, 2006
Hello,
sorry, but how can one really get the order in which FieldInfoS are returned by Type.GetFields(), as <a href="http://blogs.msdn.com/kathykam/archive/2005/09/21/472566.aspx#474548">Jeroen Frijters</a> pointed? This is an interesting question and msdn keeps silent ...
Thank you - Anonymous
March 24, 2006
Cool !
Could we force the field layout by:
[StructLayout(LayoutKind.Sequential)]
class HelloObj { ... }
Or does StructLayout only have effect only when the object is actually marshaled to the unmanaged code?