Challenge: Write a program that can display its own code in C#

This is a programming puzzle.

Write a program that can display its own code in a text box. This code should include everything in the Form1.cs file and doesn't need to include any other code such as the code in Form1.Designer.cs that creates the form's controls.

There are at least three ways to do this. See how many you can find. (Hint: One uses only normal variables and code within the program with no weird tricks.)

Over the years, I've done this in assembly language, Pascal, FORTRAN, C, C++, Delphi, Visual Basic, and C#. The key step is more or less the same in every language I've tried, at least using the "normal variables" approach.

If you find a solution, zip it up and email it to me.

To download Code Warrior's solution, which uses only program variables, click here.

To download Corey Hull's solution, which reads the project file, click here.

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

  • 7/12/2010 6:15 PM Rod Stephens wrote:
    Hints: The three main approaches are:
    1. Use reflection. This is fairly hard.

    2. Use the file system to open the project's code. This isn't too hard, although it doesn't work if you only have the compiled program.

    3. Put the code in an array and use a loop to concatenate it into a string. The trickiest part is the code that needs to write itself.

    If you have a solution, email me.
    Reply to this
    1. 12/1/2010 10:55 PM OperatorX wrote:
      Rod,

      I tried to solve this when you first posted a couple of weeks ago.

      I first tried the reflection approach, but could not access any of the private functions and variables (i.e. all had to be marked "public").

      Personally, I did not consider option #2 because this did not seem to be what you were looking for (and, well - it kinda seems like cheating to me ;-)

      Then there was #3. I did attempt to write some "polymorphic" c# once about a year ago and was for rewarded for my efforts by my AV as the code and binary were promptly jettisoned from my drive by AVG. Perhaps I will set up a "quarantine" for a fresh attempt and pursue this approach once again.

      Either way, I will be eagerly awaiting any solution more sophisticated than option #2 :-)

      Also, I hope we will see you soon and I pray that you will have success in whatever endeavor which would keep you away from this blog for so many months.

      Later,

      X
      Reply to this
  • 12/2/2010 6:41 AM Rod Stephens wrote:
    I've been working on the blog, just not this posting. Take a look at the main blog page (http://www.csharphelper.com) or the index page (http://www.csharphelper.com/howto_index.html).

    I just received a solution to this from Code Warrior yesterday. It uses method 3 (the method I originally used). I'll try to post it later today.
    Reply to this
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.