2


ive been trying adding a command system to a server application
The problem is: the command .cs files that are not inside the project but
they are compiled from another folder on runtime can't incude anything.

the compiler code:

static CompilerResults Compile(String path)
        {
            var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
            var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "DS Server.exe", true);
            parameters.GenerateExecutable = true;
            return csc.CompileAssemblyFromFile(parameters, path);
        }

external .cs code file:

using DS_Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace JMnet
{
    class command
    {
        public void run(TcpListener server,Dictionary<String,user> users , Dictionary<String, Assembly> Commands, String[] args)
        {
            foreach (user usr in users) {
                var IP = ((IPEndPoint)usr.getClient().Client.RemoteEndPoint).Address.ToString();
                Core.Log.Geneic("Players",IP+" is connected as "+usr.getName()+", ping: "+usr.getPing().ToString());
            }
        }
    }
}

The errors:

server started
Starting C-Sys 1.0
Found 2 commands to register
compiling kick
[Compiler]: c: \ csys \ cmd \ kick.cs (1,7): error CS0246: The type or namespace name DS_Server not found (missing a using statement or assembly reference?)
[Compiler]: c: \ csys \ cmd \ kick.cs (5,14): error CS0234: The type or namespace name just does not exist in the namespace System (missing an assembly reference?)
[Compiler]: c: \ csys \ cmd \ kick.cs (8,24): error CS0234: The type or name of the Tasks namespace does not exist in the namespace System.Threading (missing an assembly reference?)
[Compiler]: c: \ csys \ cmd \ kick.cs (14,25): error CS0246: The type or namespace name TcpListener not found (missing a using statement or assembly reference?)
[Compiler]: c: \ csys \ cmd \ kick.cs (14,62): error CS0246: The type or namespace name user is not found (missing a using statement or assembly reference?)
Registered kick
compiling players
[Compiler]: c: \ csys \ cmd \ players.cs (1,7): error CS0246: The type or namespace name DS_Server not found (missing a using statement or assembly reference?)
[Compiler]: c: \ csys \ cmd \ players.cs (5,14): error CS0234: The type or namespace name just does not exist in the namespace System (missing an assembly reference?)
[Compiler]: c: \ csys \ cmd \ players.cs (8,24): error CS0234: The type or name of the Tasks namespace does not exist in the namespace System.Threading (missing an assembly reference?)
[Compiler]: c: \ csys \ cmd \ players.cs (14,25): error CS0246: The type or namespace name TcpListener not found (missing a using statement or assembly reference?)
[Compiler]: c: \ csys \ cmd \ players.cs (14,62): error CS0246: The type or namespace name user is not found (missing a using statement or assembly reference?)
Registered player
NineBerry
  • 26,306
  • 3
  • 62
  • 93
JMgamerZzz TM
  • 88
  • 1
  • 9

1 Answers1

4

In this part:

var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "DS Server.exe", true);

you do need to add the assemblies where all the types that the compiler does not find are declared. You only include mscorlib.dll and System.Core.dll, but you also need to include all the other assemblies that the compiled code will need to use.

This could for example be:

var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll", "System.dll", "DS_Server.dll" }, "DS Server.exe", true);

If you cannot know beforehand, what assemblies are needed, put a configuration file besides the *.cs file that you want to compile where you specify the list of assemblies to load.


Also, in here:

var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });

you tell the compiler to use .net Framework version 3.5 which does not contain some newer features like for example System.Threading.Tasks. Add a higher version number like for example 4.0 or higher:

var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } });
NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • Hi, do you know how to include namespace within the project to the CSharpCodeProvider? – Adib Oct 27 '16 at 05:47