The foreach loop
have an unexpected behavior when the condition is true
in fact after all instruction inside if statement
, the loop breaks. I tried to comment the if statement and all works fine (iterate all the elements into the ienumerable). Someone can explain me why?
var allRef = projDefinition
.Element(msbuild + "Project")
.Elements(msbuild + "ItemGroup")
.Elements(msbuild + "COMReference")
.Where(refElem => find.Any(f => refElem.FirstAttribute.Value.ToLower().Contains(f)))
.Select(refElem => refElem);
foreach (var xElement in allRef)
{
var name = xElement.FirstAttribute.Value;
var dllPath = dllFiles.FirstOrDefault(dll => dll.ToLower().Contains(name.ToLower()));
if (dllPath != null)
{
var dllName = dllPath.Substring(dllPath.LastIndexOf('\\') + 1, dllPath.LastIndexOf('.') - dllPath.LastIndexOf('\\') - 1);
xElement.Remove();
XElement elem = new XElement(msbuild + "Reference", new XAttribute("Include", dllName));
elem.Add(new XElement(msbuild + "HintPath", HintPath.GetHintPath(dllPath)));
elem.Add(new XElement(msbuild + "EmbedInteropTypes", "False"));
projDefinition.Root.Elements(msbuild + "ItemGroup").ElementAt(0).Add(elem);
}
}
projDefinition.Save(fullProjectPath);