“Multiple assemblies with equivalent identity have been imported” in VS2015 with a Unity generated csproj

泪湿孤枕 提交于 2020-04-13 06:14:22

问题


I've started a new .NET 4.6 project in Unity 2018.1, and when I try to build it in Visual Studio 2015, I get "CS1703: Multiple assemblies with equivalent identity have been imported" for loads and loads of .NET assemblies, all of which are part of the BCL. The only code in the project is an empty class. There are no errors in the Unity console.

Easy repro steps (see version info at the end):

  1. Create a new Unity project
  2. Set the scripting runtime level to .NET 4.x in the player settings
  3. Add a new C# script
  4. Open the project in VS
  5. Try to build it

If this was a normal project I would just remove the duplicated references but this .csproj is continually regenerated by Unity.

Version information:

  • Unity: 2018.1.0f2
  • Visual Studio 2015: Update 3 (14.0.25431.01)
  • Visual Studio Tools for Unity: 3.7.0.1

回答1:


This appears to be a known issue in Unity 2018 with how it generates the Visual Studio project files. I just observed the same problem with Unity 2018.1.2f1 and Visual Studio 2015 Update 3 (14.0.25431.01).

Someone posted what appears to be the same problem on the Unity forum here. Sebastien Lebreton of Microsoft responded with a workaround until Unity fixes the problem. Add the below script into a folder named "Editor" in your project.

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


来源:https://stackoverflow.com/questions/50399959/multiple-assemblies-with-equivalent-identity-have-been-imported-in-vs2015-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!