private static void AddDependentAssembly(AssemblyRedirect redirect, XElement assemblyBindings) { var dependencyElement = assemblyBindings.Elements(DependentAssemblyElementName) .FirstOrDefault(element => IsSameAssembly(redirect, element)); if (dependencyElement == null) { dependencyElement = new XElement(DependentAssemblyElementName, new XElement(AssemblyIdentityElementName, new XAttribute(NameAttributeName, redirect.From.Name), new XAttribute(PublicKeyTokenAttributeName, redirect.From.PublicKeyToken), new XAttribute(CultureAttributeName, redirect.From.Culture) ) ); assemblyBindings.Add(dependencyElement); } bool redirectExists = dependencyElement.Elements(BindingRedirectElementName).Any(element => IsSameRedirect(redirect, element)); if (!redirectExists) { dependencyElement.Add(new XElement(BindingRedirectElementName, new XAttribute(OldVersionAttributeName, redirect.From.Version), new XAttribute(NewVersionAttributeName, redirect.To.Version) )); } }
private static bool IsSameRedirect(AssemblyRedirect redirect, XElement bindingRedirectElement) { if (bindingRedirectElement == null) { return(false); } return((string)bindingRedirectElement.Attribute(OldVersionAttributeName) == redirect.From.Version.ToString() && (string)bindingRedirectElement.Attribute(NewVersionAttributeName) == redirect.To.Version.ToString()); }
private static bool IsSameAssembly(AssemblyRedirect redirect, XElement dependentAssemblyElement) { var identity = dependentAssemblyElement.Element(AssemblyIdentityElementName); if (identity == null) { return false; } return (string)identity.Attribute(NameAttributeName) == redirect.From.Name && (string)identity.Attribute(PublicKeyTokenAttributeName) == redirect.From.PublicKeyToken && (string)identity.Attribute(CultureAttributeName) == redirect.From.Culture; }
private bool IsSameAssembly(AssemblyRedirect redirect, XElement dependentAssemblyElement) { var identity = dependentAssemblyElement.Element(AssemblyIdentityElementName); if (identity == null) { return(false); } return((string)identity.Attribute(NameAttributeName) == redirect.From.Name && (string)identity.Attribute(PublicKeyTokenAttributeName) == redirect.From.PublicKeyToken && (string)identity.Attribute(CultureAttributeName) == redirect.From.Culture); }
internal static void GenerateBindingRedirects(string configFile, AssemblyRedirect[] bindingRedirects) { XDocument configRoot = File.Exists(configFile) ? XDocument.Load(configFile) : new XDocument(); var configuration = GetOrAddElement(configRoot, ConfigurationElementName); var runtime = GetOrAddElement(configuration, RuntimeElementName); var assemblyBindings = GetOrAddElement(runtime, AssemblyBindingElementName); foreach (var redirect in bindingRedirects) { AddDependentAssembly(redirect, assemblyBindings); } using (var fileStream = File.Open(configFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { configRoot.Save(fileStream); } }
private static bool IsSameRedirect(AssemblyRedirect redirect, XElement bindingRedirectElement) { if (bindingRedirectElement == null) { return false; } return (string)bindingRedirectElement.Attribute(OldVersionAttributeName) == redirect.From.Version.ToString() && (string)bindingRedirectElement.Attribute(NewVersionAttributeName) == redirect.To.Version.ToString(); }