Monday, July 14, 2008

Simple (and funny in some way) recursy in C#

While recent work with reflections in .NET I found a funny way for recursion:

using System;

namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {
Program p =
new Program();
Console.WriteLine(p.Property);
}

public String Property {
get {
Console.WriteLine(
"Getting property, counter {0}.", counter++);
return (String)this.GetType().
GetProperty(
"Property").GetValue(this, null);
}
}
int counter = 0;
}

}
Getter of property gets PropertyInfo of itself and calls GetValue method.

No comments: