使用 Unity 生成的 csproj 在 VS2015 中“导入了多个具有相同标识的程序集”

2024-01-04

我在 Unity 2018.1 中启动了一个新的 .NET 4.6 项目,当我尝试在 Visual Studio 2015 中构建它时,我收到“CS1703:已导入具有相同标识的多个程序集”来加载和加载 .NET 程序集,所有其中是 BCL 的一部分。项目中唯一的代码是一个空类。 Unity控制台中没有错误。

简单的重现步骤(请参阅最后的版本信息):

  1. 创建一个新的Unity项目
  2. 在播放器设置中将脚本运行时级别设置为 .NET 4.x
  3. 添加新的 C# 脚本
  4. 在VS中打开项目
  5. 尝试构建它

如果这是一个正常的项目,我只会删除重复的引用,但这个 .csproj 会由 Unity 不断重新生成。

版本信息:

  • 统一:2018.1.0f2
  • Visual Studio 2015:更新 3 (14.0.25431.01)
  • 适用于 Unity 的 Visual Studio 工具:3.7.0.1

这似乎是 Unity 2018 中关于如何生成 Visual Studio 项目文件的已知问题。我刚刚在 Unity 2018.1.2f1 和 Visual Studio 2015 Update 3 (14.0.25431.01) 中观察到同样的问题。

有人在 上发布了看似相同的问题统一论坛在这里 https://forum.unity.com/threads/cant-build-unity-projects-multiple-assemblies-with-equivalent-identity-have-been-imported.524007/。 Microsoft 的 Sebastien Lebreton 给出了解决方法,直到 Unity 修复该问题。将以下脚本添加到项目中名为“Editor”的文件夹中。

using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using UnityEditor;

#if ENABLE_VSTU

using SyntaxTree.VisualStudio.Unity.Bridge;

[InitializeOnLoad]
public class ProjectFileHook
{
   // necessary for XLinq to save the xml project file in utf8
   class Utf8StringWriter : StringWriter
   {
       public override Encoding Encoding
       {
           get { return Encoding.UTF8; }
       }
   }

   static ProjectFileHook()
   {
       ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) =>
       {
           // parse the document and make some changes
           var document = XDocument.Parse(content);
           var ns = document.Root.Name.Namespace;

           document.Root
               .Descendants()
               .First(x => x.Name.LocalName == "PropertyGroup")
               .Add(new XElement(ns + "ImplicitlyExpandNETStandardFacades", "false"),
                    new XElement(ns + "ImplicitlyExpandDesignTimeFacades", "false"));

           // save the changes using the Utf8StringWriter
           var str = new Utf8StringWriter();
           document.Save(str);

           return str.ToString();
       };
   }
}

#endif
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Unity 生成的 csproj 在 VS2015 中“导入了多个具有相同标识的程序集” 的相关文章

随机推荐