0

Is there a way to have a less-strict compilation with a CodeDomProvider? I am trying to compile and load dll files into my already running program by using:

public static String Compile(string commandName, string source = "")
 {
        private static CodeDomProvider compiler = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
        private static CompilerParameters parameters = new CompilerParameters();
        parameters.GenerateExecutable = false;
        parameters.MainClass = commandName;
        parameters.OutputAssembly = dll;
        parameters.ReferencedAssemblies.Add("MCLight_.dll");
        parameters.CompilerOptions = "/optimize";
        parameters.WarningLevel = 4;
        parameters.TreatWarningsAsErrors = false;
        StreamReader sr = new StreamReader(sourcepath + "cmd" + commandName + ".cs");
        results = compiler.CompileAssemblyFromSource(parameters, sr.ReadToEnd());
        .....
 }

The problem is that errors such as:

Error #CS0122 Message: 'MCLight.Independent' is inaccessible due to its protection level Line: 1178

and

Error #CS1501 Message: No overload for method 'Find' takes '1' arguments Line: 617

are being thrown.

Now I know for a fact this class compiles fine when I compile it as part of my solution in VS. But when compiled separately it's throwing these errors. Is there a way to have the compiler ignore these errors since I know it will hook into the application just fine?

svick
  • 236,525
  • 50
  • 385
  • 514
Mike S
  • 97
  • 1
  • 8
  • In general, compilers can't simply ignore errors. An error means that the compiler wasn't able to compile the code, it doesn't know what exactly the code should do, so it can't produce the compiled assembly. – svick Aug 25 '12 at 22:19
  • Can you post a short sample code that we can use to actually reproduce this error? (Including the code of the referenced assembly.) – svick Aug 25 '12 at 22:21
  • Well I don't want to post three entire classes. What you need to know is "Independent" is another class in the same solution and the method it is trying to access is a static method. The incorrect number of arguments error is trying to access a static method in a class "Player". The special thing is this method has an optional parameter. But the compiler is treating that optional parameter as a required one. There is no error in the code. If there was then the original solution wouldn't compile. So I don't need any errors fixed, I just need to know why the compiler THINKS there is errors. – Mike S Aug 25 '12 at 22:25
  • In that case, I think the reason for the second error is that you explicitly declared you want to use the C# compiler from .Net 3.5, which doesn't support optional parameters. Not sure what could cause the first error. – svick Aug 25 '12 at 23:10
  • Yeah but if you read in the other comments on this page you'll see that I tried using 4.0 and it failed. What I'm trying to do is load external commands for my server by creating a "compile and load on the fly" option. So these external commands need to access methods of the program that is running. A DLL should be able to hook into that running process easily without any compile errors. – Mike S Aug 25 '12 at 23:44

1 Answers1

1

You could try setting the warning level lower:

      parameters.WarningLevel = 1;

But it is hard to say for sure without seeing the source of code you are compiling.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • I set the warning level to 0 and it still threw the errors. Basically Independent is another class in the same solution and the method it is trying to access is a static method. The incorrect number of arguments error is trying to access a static method in a different class. The special thing is this method has an optional parameter. But the compiler is treating that optional parameter as a required one. Is there a reason this compiler doesn't have the same rules as the VS2010 one? – Mike S Aug 25 '12 at 22:01
  • Weren't optional parameters not added till 4.0 -- try using 4.0 instead of 3.5 – Hogan Aug 25 '12 at 22:03
  • Tried that and it doesn't seem to know what 4.0 is and it throws an error: = new CSharpCodeProvider(new Dictionary() { { "CompilerVersion", "v4.0" } }); – Mike S Aug 25 '12 at 22:25