コード例 #1
0
        /// <summary>
        /// An event handler for resolving Silverlight framework assembly versioning.
        /// </summary>
        /// <remarks>
        /// When a previous version of a Silverlight assembly is sought, the targeted version
        /// of that Silverlight assembly will be returned.
        /// </remarks>
        /// <param name="sender">The event sender.</param>
        /// <param name="args">The assembly resolution event arguments.</param>
        /// <returns>The <see cref="Assembly"/> from the targeted version of Silverlight, or <c>null</c>.</returns>
        private static Assembly ResolveFrameworkAssemblyVersioning(object sender, ResolveEventArgs args)
        {
            FrameworkManifest frameworkManifest = (FrameworkManifest)AppDomain.CurrentDomain.GetData(AppDomainUtilities.FrameworkManifestKey);
            System.Diagnostics.Debug.Assert(frameworkManifest != null, "The FrameworkManifest must have been set on the AppDomain");

            AssemblyName requestedAssembly = new AssemblyName(args.Name);

            // If the requested assembly is a System assembly and it's an older version
            // than the framework manifest has, then we'll need to resolve to its newer version
            bool isOldVersion = requestedAssembly.Version.CompareTo(frameworkManifest.SystemVersion) < 0;

            if (isOldVersion && requestedAssembly.IsSystemAssembly())
            {
                // Now we need to see if the requested assembly is part of the framework manifest (as opposed to an SDK assembly)
                var silverlightAssembly = (from assembly in frameworkManifest.Assemblies
                                           where assembly.Name == requestedAssembly.Name
                                           select assembly).SingleOrDefault();

                // If the assembly is part of the framework manifest, then we need to "redirect" its resolution
                // to the current framework version.
                if (silverlightAssembly != null)
                {
                    // Find the Silverlight framework assembly from the already-loaded assemblies
                    var matches = from assembly in AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies()
                                  let assemblyName = assembly.GetName()
                                  where assemblyName.Name == silverlightAssembly.Name
                                     && assemblyName.GetPublicKeyToken().SequenceEqual(silverlightAssembly.PublicKeyTokenBytes)
                                     && assemblyName.Version.CompareTo(silverlightAssembly.Version) == 0
                                  select assembly;

                    return matches.SingleOrDefault();
                }
            }

            return null;
        }