0

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);
            }
        }
The Flash
  • 63
  • 1
  • 6

1 Answers1

0

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 ...");
dtb
  • 213,145
  • 36
  • 401
  • 431
  • Now I get an Error saying : assembly is not a valid location for this declaration.Valid attribute locations for this declarations are"type" – The Flash Jun 15 '13 at 21:04
  • Global attributes like `AssemblyDescription` must appear in the source code after any top-level `using` directives and before any type or `namespace` declarations. See [Global Attributes (C# Programming Guide)](http://msdn.microsoft.com/en-us/library/284c1c4s(v=vs.90).aspx) – dtb Jun 15 '13 at 21:07
  • Did like you said, now I get this Error : The type or NameSpace name "AssemblyDescription" could not be found (are you missing a using directive or an assembly reference?) – The Flash Jun 15 '13 at 21:12
  • Well, are you missing a using directive or an assembly reference? The [`AssemblyDescription` attribute](http://msdn.microsoft.com/en-us/library/system.reflection.assemblydescriptionattribute.aspx) is in the `System.Reflection` namespace in the `mscorlib.dll` assembly. – dtb Jun 15 '13 at 21:20
  • Ok sir I did everything you said. Now I dont get any error, but the Assembly Discription is not updated with the text of the textbox. If I generate the .exe, it has no file description... – The Flash Jun 15 '13 at 21:20
  • See [What happend to AssemblyDescription in Windows 7?](http://stackoverflow.com/questions/2326998) – dtb Jun 15 '13 at 21:26
  • Sorry maybe I didnt verbalize my problem right^^ I can see the description, but its Empty... – The Flash Jun 15 '13 at 21:29
  • Read [What happend to AssemblyDescription in Windows 7](http://stackoverflow.com/questions/2326998) again. The [`AssemblyDescription` attribute](http://msdn.microsoft.com/en-us/library/system.reflection.assemblydescriptionattribute.aspx) does not show up in the properties dialog in Windows Explorer. The "File description" field is set by the [`AssemblyTitle` attribute](http://msdn.microsoft.com/en-us/library/system.reflection.assemblytitleattribute.aspx). – dtb Jun 15 '13 at 21:33
  • I though that using CodeDOM to properly escape the string (though `CodePrimitiveExpression` and `GenerateCodeFromExpression()`) would be cleaner, but all it does is to make the code more complex. Though if the code used the CodeDOM object model, it would make sense. – svick Jun 15 '13 at 21:33