-1

What I mean is, could one possibly make a program that does the equivalent of

public class PrintOwnSourceCode
{
   public static void Main ( )
   {
      System.Console.WriteLine([something]);
      // prints "public class PrintOwnSourceCode { public static void Main ( ) { ... } }" 
   }
}

???

And would that be an example of reflection?

Subpar Web Dev
  • 3,210
  • 7
  • 21
  • 35
  • A compiled assembly does not contain its source code - you can produce source functionally equivalent (and in many cases statement-by-statement identical in .Net), via decompilation, but looking for a universal solution that will translate compiled code into source code that is identical to the original source isn't possible. You *can* build quines, whose sole purpose is to print their own source code, but this is more of a puzzle than a generally useful concept. – Preston Guillot Dec 28 '15 at 19:07
  • Do you have a reason for doing this? Or just curious? The short answer is yes it's possible, e.g. one way is to copy the source code with your deploy then read from it. – Quantumplate Dec 28 '15 at 19:10

2 Answers2

2

Somewhat.

Decompilers can do something similar to this: I just decompiled a decompiler so I could use it to decompile itself

.NET Decompilers, like [.NET Reflector] (http://www.red-gate.com/products/dotnet-development/reflector/) and dotPeek are capable of reflecting upon a .NET assembly and generating files that resemble the source code. It will not look exactly like the source code because compiling and decompiling is kind of like translating English to French and then back to English--the results are not always guaranteed to be 1:1 as Google Translate can demonstrate. Information, like whitespace, that are for easy reading but not required by the compiler will be lost in the decompilation process. So, your application could decompile itself or invoke an external decompiler to print itself.


Aside

In compiled languages, the compiled code does not have direct access to the source code. (Companies don't typically ship the source code with the compiled code to customers. They only ship the compiled executable.) When it comes to parsed languages, like JavaScript, it's a whole different story. Because the source must be available to the runtime so that it can be parsed and run, the code can always find it's own source file, open it, and print it out.

MattTannahill
  • 606
  • 4
  • 11
0

This was answered here.

The short answer is that you cannot print it via reflection.

If you want to print out the file, then you will need to load in the source file itself (and have the file available).

Community
  • 1
  • 1
Nathan C
  • 205
  • 2
  • 10
  • Then how does ASP.NET MVC do the thing where it looks at names of controller classes within the program and uses those names to configure the routing? – Subpar Web Dev Dec 28 '15 at 18:14
  • 1
    @SubparWebDev - Reflection allows one to see the metadata of the classes (class name, properties, methods, etc.) It does not expose the source code. I am not aware of all the details for how ASP.NET MVC works under the covers (though there is plenty of material online). I would say that it does not require the source code to perform its work. It can use reflection to determine the Controller name and the various methods that are ActionResults. – Nathan C Dec 28 '15 at 18:24