Example #1
0
        /// <summary>
        /// Get the target dot net framework string for the assembly
        /// </summary>
        /// <param name="path">Path of the assembly file</param>
        /// <returns>String representation of the the target dot net framework e.g. .NETFramework,Version=v4.0 </returns>
        internal static string GetTargetFrameworkVersionString(string path)
        {
            Debug.Assert(!string.IsNullOrEmpty(path));

            var setupInfo = new AppDomainSetup();

            setupInfo.ApplicationBase = Path.GetDirectoryName(Path.GetFullPath(path));

            // In Dev10 by devenv uses its own app domain host which has default optimization to share everything.
            // Set LoaderOptimization to MultiDomainHost which means:
            //   Indicates that the application will probably host unique code in multiple domains,
            //   and the loader must share resources across application domains only for globally available (strong-named)
            //   assemblies that have been added to the global assembly cache.
            setupInfo.LoaderOptimization = LoaderOptimization.MultiDomainHost;

            if (File.Exists(path))
            {
                AppDomain ad = null;
                try
                {
                    ad = AppDomain.CreateDomain("Framework Version String Domain", null, setupInfo);

                    var assemblyLoadWorker    = typeof(AssemblyLoadWorker);
                    AssemblyLoadWorker worker = null;
                    if (assemblyLoadWorker.Assembly.GlobalAssemblyCache)
                    {
                        worker = (AssemblyLoadWorker)ad.CreateInstanceAndUnwrap(
                            assemblyLoadWorker.Assembly.FullName,
                            assemblyLoadWorker.FullName,
                            false, BindingFlags.Default, null,
                            null, null, null);
                    }
                    else
                    {
                        // This has to be LoadFrom, otherwise we will have to use AssemblyResolver to find self.
                        worker = (AssemblyLoadWorker)ad.CreateInstanceFromAndUnwrap(
                            assemblyLoadWorker.Assembly.Location,
                            assemblyLoadWorker.FullName,
                            false, BindingFlags.Default, null,
                            null, null, null);
                    }

                    return(worker.GetTargetFrameworkVersionStringFromPath(path));
                }
                finally
                {
                    if (ad != null)
                    {
                        AppDomain.Unload(ad);
                    }
                }
            }

            return(string.Empty);
        }
Example #2
0
        /// <summary>
        /// Finds platform and .Net framework version for a given test container.
        /// If there is an error while infering this information, defaults (AnyCPU, None) are returned
        /// for faulting container.
        /// </summary>
        /// <param name="testSource"></param>
        /// <returns></returns>
        public static KeyValuePair <Architecture, FrameworkVersion> GetFrameworkVersionAndArchitectureForSource(string testSource)
        {
            ValidateArg.NotNullOrEmpty(testSource, "testSource");

            var sourceDirectory = Path.GetDirectoryName(testSource);
            var setupInfo       = new AppDomainSetup();

            setupInfo.ApplicationBase    = sourceDirectory;
            setupInfo.LoaderOptimization = LoaderOptimization.MultiDomainHost;
            AppDomain ad = null;

            try
            {
                ad = AppDomain.CreateDomain("Multiargeting settings domain", null, setupInfo);

                Type assemblyLoadWorker   = typeof(AssemblyLoadWorker);
                AssemblyLoadWorker worker = null;

                // This has to be LoadFrom, otherwise we will have to use AssemblyResolver to find self.
                worker = (AssemblyLoadWorker)ad.CreateInstanceFromAndUnwrap(
                    assemblyLoadWorker.Assembly.Location,
                    assemblyLoadWorker.FullName,
                    false, BindingFlags.Default, null,
                    null, null, null);

                string procArchType;
                string frameworkVersion;
                worker.GetPlatformAndFrameworkSettings(testSource, out procArchType, out frameworkVersion);

                Architecture     targetPlatform  = (Architecture)Enum.Parse(typeof(Architecture), procArchType);
                FrameworkVersion targetFramework = FrameworkVersion.Framework45;
                switch (frameworkVersion.ToUpperInvariant())
                {
                case "V4.5":
                    targetFramework = FrameworkVersion.Framework45;
                    break;

                case "V4.0":
                    targetFramework = FrameworkVersion.Framework40;
                    break;

                case "V3.5":
                case "V2.0":
                    targetFramework = FrameworkVersion.Framework35;
                    break;

                default:
                    targetFramework = FrameworkVersion.None;
                    break;
                }
                if (EqtTrace.IsVerboseEnabled)
                {
                    EqtTrace.Verbose("Inferred Multi-Targeting settings:{0} Platform:{1} FrameworkVersion:{2}", testSource, targetPlatform, targetFramework);
                }
                return(new KeyValuePair <Architecture, FrameworkVersion>(targetPlatform, targetFramework));
            }
            finally
            {
                if (ad != null)
                {
                    AppDomain.Unload(ad);
                }
            }
        }
Example #3
0
        public static bool?DoesReferencesAssembly(string source, AssemblyName referenceAssembly)
        {
            try
            {
                ValidateArg.NotNullOrEmpty(source, "source");
                ValidateArg.NotNull(referenceAssembly, "referenceAssembly");

                Debug.Assert(!string.IsNullOrEmpty(source));

                var referenceAssemblyName           = referenceAssembly.Name;
                var referenceAssemblyPublicKeyToken = referenceAssembly.GetPublicKeyToken();

                var setupInfo = new AppDomainSetup();
                setupInfo.ApplicationBase = Path.GetDirectoryName(Path.GetFullPath(source));

                // In Dev10 by devenv uses its own app domain host which has default optimization to share everything.
                // Set LoaderOptimization to MultiDomainHost which means:
                //   Indicates that the application will probably host unique code in multiple domains,
                //   and the loader must share resources across application domains only for globally available (strong-named)
                //   assemblies that have been added to the global assembly cache.
                setupInfo.LoaderOptimization = LoaderOptimization.MultiDomainHost;

                AppDomain ad = null;
                try
                {
                    ad = AppDomain.CreateDomain("Dependency finder domain", null, setupInfo);

                    var assemblyLoadWorker    = typeof(AssemblyLoadWorker);
                    AssemblyLoadWorker worker = null;
                    if (assemblyLoadWorker.Assembly.GlobalAssemblyCache)
                    {
                        worker = (AssemblyLoadWorker)ad.CreateInstanceAndUnwrap(
                            assemblyLoadWorker.Assembly.FullName,
                            assemblyLoadWorker.FullName,
                            false, BindingFlags.Default, null,
                            null, null, null);
                    }
                    else
                    {
                        // This has to be LoadFrom, otherwise we will have to use AssemblyResolver to find self.
                        worker = (AssemblyLoadWorker)ad.CreateInstanceFromAndUnwrap(
                            assemblyLoadWorker.Assembly.Location,
                            assemblyLoadWorker.FullName,
                            false, BindingFlags.Default, null,
                            null, null, null);
                    }

                    return(worker.CheckAssemblyReference(source, referenceAssemblyName, referenceAssemblyPublicKeyToken));
                }
                finally
                {
                    if (ad != null)
                    {
                        AppDomain.Unload(ad);
                    }
                }
            }
            catch
            {
                return(null); // Return null if something goes wrong.
            }
        }