I don't know why this error pops up I don't have anything declared out of class my full code is this:
using System;
using Microsoft.CSharp;
using System.IO;
using System.CodeDom.Compiler;
using System.Text;
namespace test
{
public class comp
{
static void Main()
{
string code="0x4d,0x41,0x2......"; //and soo onnn
string Arch = " /platform:x86 /optimize";
string filePath = "C:/menmon.exe";
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("System.Core.dll");
parameters.GenerateInMemory = false;
parameters.GenerateExecutable = true;
parameters.IncludeDebugInformation = false;
parameters.ReferencedAssemblies.Add("mscorlib.dll");
parameters.ReferencedAssemblies.Add("System.dll");
parameters.OutputAssembly = filePath;
parameters.CompilerOptions = "/target:exe" + Arch;
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
if (results.Errors.HasErrors)
{
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors)
{
sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
}
throw new InvalidOperationException(sb.ToString());
}
}
}
}
Compiled this using cmd on Windows machine on .Net framework64 v4.0.30319 error happens after compilation(on compilation no error is shown) when I run the program this error pops up:
Unhandled Exception: System.InvalidOperationException: Error (CS0116): A namespace cannot directly contain members such as fields or methods
at comp.Main()
I ran csc.exe like this
csc /t:exe /out:"C:\\output.exe" "C:\\input.cs"
Not sure will this help you but can you please try to add namespace of your App just up of the class so it looks like this:
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Also you are missing semi colon on the last line in main function.
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.