gpt4 book ai didi

c# - CompileAssemblyFromFile c# 属性 7.2 失败

转载 作者:行者123 更新时间:2023-11-30 22:58:42 26 4
gpt4 key购买 nike

我曾尝试用文件编译代码,但它有一些错误。错误:1.global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)不 ';'最后

2.public string Name { get;放; } = ""; '=' 是不允许的

代码属性是C# 7.2+,我的项目语言版本是7.3,.Net FrameWork 4.6.1,有什么想法吗?

  CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters parameters = new CompilerParameters();
parameters.CompilerOptions = "/target:library /optimize /warn:0";
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
parameters.IncludeDebugInformation = true;
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("protobuf-net.dll");


string[] csharp_files = Tool.GetAllFileNamesByPath(Options.Global.temp_cs_dir, new string[]{ "cs" }).ToArray<string>();

CompilerResults results = provider.CompileAssemblyFromFile(parameters, csharp_files);

你的cs文件是

// This file was generated by a tool; you should avoid making direct changes.
// Consider using 'partial classes' to extend these types
// Input: Role.proto

#pragma warning disable CS1591, CS0612, CS3021, IDE1006
namespace DataTable
{

[global::ProtoBuf.ProtoContract()]
public partial class Role : global::ProtoBuf.IExtensible
{
private global::ProtoBuf.IExtension __pbn__extensionData;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
=> global::ProtoBuf.Extensible.GetExtensionObject(ref __pbn__extensionData, createIfMissing);

[global::ProtoBuf.ProtoMember(1)]
public int Id { get; set; }

[global::ProtoBuf.ProtoMember(2)]
[global::System.ComponentModel.DefaultValue("")]
public string Name { get; set; } = "";

[global::ProtoBuf.ProtoMember(3)]
public bool IsBool { get; set; }

[global::ProtoBuf.ProtoMember(4, IsPacked = true)]
public int[] Lst { get; set; }

[global::ProtoBuf.ProtoMember(5)]
[global::ProtoBuf.ProtoMap]
public global::System.Collections.Generic.Dictionary<int, string> Dic { get; set; } = new global::System.Collections.Generic.Dictionary<int, string>();

}

}

#pragma warning restore CS1591, CS0612, CS3021, IDE1006

最佳答案

这听起来像是 CodeDOM 不支持最新编译器版本的限制?例如,当使用 CSharpCodeProvider 时,您需要手动指定 "CompilerVersion" 设置,而且它只能达到 pre-Roslyn 水平。

由于您使用的是“protogen”,请注意“protogen”支持低级编译器版本:

enter image description here

(为了简洁起见,它不是明确的 - 但像 #pragma 更改之类的事情在您更改语言版本时得到正确处理)

我猜 C# 3 是 CodeDOM 的不错选择。

这可以通过 Web UI 使用命令行工具指定,也可以在 .proto 文件中指定 using custom options .

关于c# - CompileAssemblyFromFile c# 属性 7.2 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52853062/

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