Welcome to MSDN Blogs Sign in | Join | Help

Kathy Kam

Reflection on the CLR and .NET
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

Posted: Wednesday, September 21, 2005 2:48 PM by KathyKam
Filed under:

Comments

Eric W said:

lol. That is one of the cleverest ways I've seen to do "Hello World".
# September 27, 2005 4:07 PM

Jeroen Frijters said:

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 :-)
# September 27, 2005 4:16 PM

Hugo Batista said:

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 :)
# September 27, 2005 6:40 PM

Mohammed said:


Nice way to do a hello world Kathy,All the best at the CLR team
# September 27, 2005 6:54 PM

Marlun said:

Hello! =)

Happy to see that you've started a blog, another one for my list and more information to gather :)

Welcome!

Marlun, Sweden
# September 27, 2005 7:53 PM

KathyKam said:

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
# September 27, 2005 8:42 PM

Dmitry Shaporenkov said:

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
# September 28, 2005 6:12 AM

Themes said:

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);
}
# September 29, 2005 9:08 AM

Themes said:

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);
}
# September 29, 2005 9:09 AM

SN said:

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
# October 15, 2005 2:47 PM

esteewhy said:

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
# February 13, 2006 3:41 PM

Tadej said:

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?
# March 24, 2006 6:05 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker