-2

Is there any way to print or use in anyway an actual code piece or snip? Like if the code is-

if(BA == True)
{
    Console.Writeline("Okay") 
};

Then if it's possible to actually print that entire text "if...} ;" to the console or excel / word file etc.. so the console output will be-

if(BA == True)
{ 
    Console.Writeline("Okay") 
}; 

for example.. Sounds like very simple and basic if possible at all, but i couldn't find it anywhere with many search combos..

Thanks..

maccettura
  • 10,514
  • 3
  • 28
  • 35
  • If you'd really want the line to print the line itself, it sounds you're looking for a sort of [quine](https://en.wikipedia.org/wiki/Quine_(computing))... – AKX Aug 22 '22 at 17:54
  • 4
    Well the obvious way to do it would be to write `Console.WriteLine(@"if(BA == True){ Console.Writeline(""Okay"") };");`. But something tells me you are looking for something better in some way. What are you trying to accomplish? – John Wu Aug 22 '22 at 17:56
  • You'd have to give an example of how you are getting this code snippet. Is it in the same file? A different file? – Jonesopolis Aug 22 '22 at 18:01
  • Most language allow you to write self-printing program - well known code-golf style exercise. I.e. https://www.bing.com/search?q=C%23+selfprinting+code . Absolutely non-practical exercise and I'm not sure this topic is suitable as question for SO. – Alexei Levenkov Aug 22 '22 at 18:04
  • Check: https://stackoverflow.com/a/1100265/18667225 – Markus Aug 22 '22 at 18:38
  • @Jonesopolis simply like string sCode = "if(CustomFunction(t,7) == 1) { MessageBox.Show("PositiveValue"); }" Then if i use it in a code i add some mark to the system like " if(bool0 == true) { #code sCode #endcode } " so it uses it as code but i can also add right after " Console.WriteLine(sCode); " and the output of course will be the entire string text in the Console- if(CustomFunction(t,7) == 1) { MessageBox.Show("PositiveValue"); } , and popup the message if the condition is met.. – Dex DBadAss Aug 24 '22 at 20:25

2 Answers2

0

As far as I know there's not really an easy way to do this in C#. There are a couple ways around it:

  1. Distribute your source with your program in a known location, then in the code open the source file and read out the contents parsing it for the parts you need.

  2. Distribute a "prepared" version of source, saving only the parts you need in some file, possibly formatted using JSON or something to easily get the important bits. This preparation could be done as part of the build process.

  3. On the cooler yet slightly more ridiculous side of things, C# code is normally compiled to IL, which can very effectively be decompiled (for example, in ILSpy). It might just be that there's a decompilation library out there you can use on the source of the running program, and then there's no need to also package a source file. Note that this won't have great results if your build process has obfuscation. If that's the case, you'll also need to put the decompiled code through a deobfuscator that works with your obfuscator before outputting it to the user.

Out of curiosity, why do you want to do this? I think I tried to find a way to do it too a while back but for the life of me can't remember why.

Traveller
  • 604
  • 6
  • 16
0

Yes, that's possible in 8-20 lines of code.

Here's the code:

using System;
using System.IO;
using System.Reflection;

namespace CodeViewer
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(Read());
        }

        public static string Read()
        {
            string res = "";

            Assembly a = Assembly.GetExecutingAssembly();
            Stream stream = a.GetManifestResourceStream("CodeViewer.Program2.cs");
            StreamReader reader = new StreamReader(stream);

            res = reader.ReadToEnd();
        
            return res;
        }
    }
}

CodeViewer is the namespace of the project.

Here's a necessary part of the project's file (.csproj):

<ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <EmbeddedResource Include="Program.cs">
        <Link>Program2.cs</Link>
    </EmbeddedResource>
</ItemGroup>

I added the link to the Program.cs where is my program's code. After, I accessed it in Read method.

Dragon-LV
  • 1
  • 1
  • 4