简体   繁体   中英

How to use " in CodeDom Source Code?

I want to append this Line to the source code which will be compiled :

Temp.AppendLine(@"[assembly: AssemblyDescription("[DESCRIPTION]"]);"); 

But you see the [DESCRIPTION] is not in the string too... Instead of the [DISCRIPTION] I want to use the input of an TextBox. How can I do that?

        private void button1_Click(object sender, EventArgs e)
    {
        SaveFileDialog d = new SaveFileDialog();
        d.Filter = "Executable (*.exe)|*.exe";
        if (d.ShowDialog() == DialogResult.OK)
        {

            String InputCode = String.Empty;
            String regcode = String.Empty;
            //Unser TestCode, in dem Wir ein MessageBox aufrufen
            InputCode = "MessageBox.Show((1 + 2 + 3).ToString());";

            System.CodeDom.Compiler.CodeDomProvider CodeDomProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
            //Parameter für die Compilierung, wie die einzubindenen Bibliotheken usw.
            System.CodeDom.Compiler.CompilerParameters CompilerParameters = new System.CodeDom.Compiler.CompilerParameters();
            CompilerParameters.ReferencedAssemblies.Add("System.dll");
            CompilerParameters.OutputAssembly = d.FileName;
            CompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            CompilerParameters.CompilerOptions += "/target:winexe" + " " + "/win32icon:" + "\"" + textBox6.Text + "\"";
            CompilerParameters.GenerateExecutable = true;
            CompilerParameters.GenerateInMemory = false;

            //Über den StringBuilder wird der Code zusammengesetzt
            StringBuilder Temp = new StringBuilder();
            Temp.AppendLine(@"using System;");
            Temp.AppendLine(@"using System.Windows.Forms;");
            Temp.AppendLine(@"using System.Diagnostics;");
            Temp.AppendLine(@"namespace RunTimeCompiler{");
            Temp.AppendLine(@"public class Test{");
            Temp.AppendLine(@"public static void Main(){");

            Temp.AppendLine(@InputCode);
            Temp.AppendLine(@"}");
            Temp.AppendLine(@"public void Ergebnis(){");
            Temp.AppendLine(@"}}}");


            //Compilieren
            System.CodeDom.Compiler.CompilerResults CompilerResults = CodeDomProvider.CompileAssemblyFromSource(CompilerParameters, Temp.ToString());
            //Auf CompilerFehler prüfen
            if (CompilerResults.Errors.Count > 0)
            {
                MessageBox.Show(CompilerResults.Errors[0].ErrorText, "Fehler bei Laufzeitkompilierung", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                MessageBox.Show("Done", "Finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

Take the TextBox input, convert it to a C# string literal and place it within the string that contains the attribute code:

string description = textBox1.Text;

string descriptionAsCsharpStringLiteral =
    "@\"" + description.Replace("\"", "\"\"") + "\"";

string attribute =
    "[assembly: AssemblyDescription(" + descriptionAsCsharpStringLiteral + ")]";

Temp.AppendLine("using System.Reflection;")
    .AppendLine(attribute)
    .AppendLine("namespace ...");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM