public void Constructing()
 {
     var an = new AssemblyNameInGAC("Accessibility, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
     Assert.AreEqual("Accessibility", an.Name);
     Assert.AreEqual("4.0.0.0", an.Version);
     Assert.AreEqual("neutral", an.Culture);
     Assert.AreEqual("b03f5f7f11d50a3a", an.PublicKeyToken);
 }
 public void ShortString()
 {
     var an = new AssemblyNameInGAC("Accessibility");
     Assert.IsEmpty(an.Version);
     Assert.IsEmpty(an.Culture);
     Assert.IsEmpty(an.PublicKeyToken);
     Assert.AreEqual("Accessibility", an.Generate());
 }
    public static void ReplaceReference(string csproj_file, string reference_name, bool force)
    {
        bool bWasRemoved = false;
        var newname = new AssemblyNameInGAC(reference_name);
        var stream = new MemoryStream(File.ReadAllBytes(csproj_file)); // cache file in memoty
        var document = XDocument.Load(stream);
        var xmlNamespaceManager = new XmlNamespaceManager(new NameTable());
        xmlNamespaceManager.AddNamespace("ns", namespaceName);

        // remove ProjectReference
        var itemsToRemove2 = new List<XElement>();
        IEnumerable<XElement> listOfReferences2 = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:ProjectReference[@Include]", xmlNamespaceManager);
        foreach (var el in listOfReferences2)
        {
            string assRef = el.Attribute("Include").Value;
            if (string.Compare(newname.Name, assRef) == 0)
            {
                itemsToRemove2.Add(el);
            }
        }
        foreach (var item in itemsToRemove2)
        {
            Console.WriteLine($"ProjectReference removed {item.ToString()}");
            item.Remove();
            bWasRemoved = true;
        }

        var itemsToRemove = new List<XElement>();
        IEnumerable<XElement> listOfReferences = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:Reference[@Include]", xmlNamespaceManager);
        foreach (var el in listOfReferences)
        {
            string assRef = el.Attribute("Include").Value;
            var name = new AssemblyNameInGAC(assRef);
            if (string.Compare(newname.Name, name.Name) == 0)
            {
                itemsToRemove.Add(el);
            }
        }
        foreach (var item in itemsToRemove)
        {
            Console.WriteLine($"Reference removed {item.ToString()}");
            item.Remove();
            bWasRemoved = true;
        }
        XElement group = document.XPathSelectElement("/ns:Project/ns:ItemGroup", xmlNamespaceManager);
        if (group == null)
        {
            // insert new item group
            throw new NotImplementedException();
        }

        // insert new reference
        if (force || bWasRemoved)
        {
            XNamespace ns = document.Root.Name.Namespace;
            var newEl = new XElement(ns + "Reference");
            newEl.Add(new XAttribute("Include", newname.Generate()));
            group.Add(newEl);
            document.Save(csproj_file);
        }
    }