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.

This entry was posted in challenges, miscellany, variables. Bookmark the permalink.

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

  1. Rod Stephens says:

    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.

  2. OperatorX says:

    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

  3. Rod Stephens says:

    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.

  4. Nikhil Khot says:

    Firstly create one text file and copy all the content of Form1.cs after that read it in a textbox.
    simple 🙂

  5. Nikhil Khot says:

    richTextBox1.Text = File.ReadAllText(@”D:\Project\C sharp trials\displaycode\displaycode\Form1.cs”);
    Try this…
    🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.