Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am currently using VS2022, and I am reading the text file, which is a C# code in that.

I want to convert the txt file to a C# compiler code.

What I have tried:

C#
string src =
@"using System;

        namespace Test{
          public class TestClass {

            public TestClass() {}
          }
        }";

        CSharpCodeProvider provider = new CSharpCodeProvider();

        CompilerParameters cp = new CompilerParameters();
        cp.GenerateInMemory = true;
                    cp.GenerateExecutable = false;
                    //cp.ReferencedAssemblies.Add("System.dll");
                    CompilerResults cr = 
                    provider.CompileAssemblyFromSource(cp, src);
                    if (cr.Errors.Count > 0)
                    {
                        foreach (CompilerError ce in cr.Errors)
                        {
                            Console.WriteLine(ce.ErrorText);
                        }
}
                    else
{
    Type t = cr.CompiledAssembly.GetType("Test.TestClass");
    object o = Activator.CreateInstance(t);
}
Posted
Updated 6-Nov-23 0:59am
v2
Comments
Richard Deeming 6-Nov-23 6:58am    
And?

You seem to have forgotten to ask a question.

Click the green "Improve question" link and update your question to explain precisely what the problem is, what you have tried, and where you are stuck. Include the full details of any errors.
Dave Kreskowiak 6-Nov-23 9:25am    
Your sample code to compile has no actual code in it, so I'd start there with whatever problem you're not describing in your question.
PIEBALDconsult 6-Nov-23 13:55pm    
Rename it.
[no name] 7-Nov-23 14:59pm    
Display the compiler results:

https://learn.microsoft.com/en-us/dotnet/api/system.codedom.compiler.codedomprovider.compileassemblyfromsource?view=dotnet-plat-ext-7.0

https://learn.microsoft.com/en-us/dotnet/api/system.codedom.compiler.compilerresults?view=dotnet-plat-ext-7.0

1 solution

Just remove the first line and the @" at the beginning of the second line, and the "; at the end of line 9. You can then rename the file to whatever source name you need, and compile it.
The final text should be:
C#
using System;

        namespace Test{
          public class TestClass {

            public TestClass() {}
          }
        }

        CSharpCodeProvider provider = new CSharpCodeProvider();

        CompilerParameters cp = new CompilerParameters();
        cp.GenerateInMemory = true;
                    cp.GenerateExecutable = false;
                    //cp.ReferencedAssemblies.Add("System.dll");
                    CompilerResults cr = 
                    provider.CompileAssemblyFromSource(cp, src);
                    if (cr.Errors.Count > 0)
                    {
                        foreach (CompilerError ce in cr.Errors)
                        {
                            Console.WriteLine(ce.ErrorText);
                        }
}
                    else
{
    Type t = cr.CompiledAssembly.GetType("Test.TestClass");
    object o = Activator.CreateInstance(t);
}
 
Share this answer
 
Comments
Maciej Los 16-Nov-23 15:38pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900