gpt4 book ai didi

C# csharpcompiler 编译器程序集无法导入任何内容

转载 作者:行者123 更新时间:2023-11-30 20:37:07 27 4
gpt4 key购买 nike


我一直在尝试向服务器应用程序添加命令系统
问题是:不在项目内但
的命令.cs文件它们是在运行时从另一个文件夹编译的,不能包含任何东西。

编译器代码:

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);
}

外部 .cs 代码文件:

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());
}
}
}
}

错误:

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

最佳答案

这部分:

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

您确实需要添加声明编译器未找到的所有类型的程序集。您只包括 mscorlib.dll 和 System.Core.dll,但您还需要包括编译后的代码需要使用的所有其他程序集。

例如,这可能是:

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

如果您事先无法知道需要哪些程序集,请在要编译的 *.cs 文件之外放置一个配置文件,您可以在其中指定要加载的程序集列表。


另外,在这里:

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

您告诉编译器使用 .net Framework 3.5 版,它不包含一些较新的功能,例如 System.Threading.Tasks。添加更高的版本号,例如 4.0 或更高版本:

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

关于C# csharpcompiler 编译器程序集无法导入任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com