1

As a little experiment, I wrote some code that edits one of its own class files.

Specifically:

namespace Dynamic
{
    class DynamicFunction
    {
        public double Function(double field1, double field2)
        {
            //Reference
            return field1;
            //EndReference
        }
    }
}

I am dynamically editing the 'Function' method through the program itself. So, if one wanted to return field1 % field 2, they would simply type in 'return field1 % field2;', hit a button, and a filestream would edit the actual source code.

However, it seems that this has no effect on the class file until the program is closed, then it takes effect when it is reopened. What's the dealio?

Here's the writing code:

System.IO.File.Delete(path);
        using (System.IO.StreamWriter W = new System.IO.StreamWriter(path))
        {
            foreach (string s in C)
            {
                if (!s.Contains("return"))
                {
                    W.WriteLine(s);
                }
                if (s.Contains("return"))
                {
                    W.WriteLine(textBox3.Text);
                }
            }
        }
        int r = 0;
wvn
  • 624
  • 5
  • 12
  • C# is compiled. Changes to source won't take effect until you've recompiled the source and reran the application. – Chris Jul 28 '16 at 22:14
  • Thanks. Is there a way to get around this, other than having the user restart the program? – wvn Jul 28 '16 at 22:24
  • No. You can't make C# an interpreted program. You can fake it by taking a string (like "field1 % field2"), compiling it dynamically, and evaluating it, but you can't just change the source code of the application and expect it to do anything until the application is recompiled. – itsme86 Jul 28 '16 at 22:51
  • There are obviously tons of question on dynamically creating/compiling code and "C# scripting"... It is very strange that your favorite search engine found nothing on the topic... – Alexei Levenkov Jul 28 '16 at 23:16
  • You could run a program, which edits some source, then compiles it, and then starts a new process executing what was compiled. However, I would seek out whether what you're doing is the best solution to fulfill your requirement. – Chris Jul 28 '16 at 23:33

1 Answers1

1

The reason behind this is the fact that when you run your program through your IDE, or compile it through the command line, it is "compiled." Therefore, when you edit the file, you're editing the source code. However, once the program is compiled it won't use the source code anymore. So when you restart your program through your IDE, the changes take effect because you're recompiling the program. As far as I'm aware, there's no way to edit a program's actual code through the program itself. The OS won't allow you to edit a program's binary data while it is running. A workaround for this that does not involve editing the actual code could be using System.Reflection, a namespace that allows to get and invoke methods through strings. You could also see this post for a different approach. With these, you could probably dynamically invoke methods like object.Equals() which might be able to serve your needs. Good luck!

Community
  • 1
  • 1
Douglas Dwyer
  • 562
  • 7
  • 14