Beispiel #1
0
        /// <summary>
        /// Detects the platform.
        /// </summary>
        /// <param name="assemblyPath">The assembly path.</param>
        /// <returns>CPU target</returns>
        public static CpuTarget DetectPlatform(string assemblyPath)
        {
            if (string.IsNullOrEmpty(assemblyPath))
            {
                throw new ArgumentException("Argument null or empty", "assemblyPath");
            }

            AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;

            setup.ApplicationBase = Path.GetDirectoryName(assemblyPath);
            string    domainName = Guid.NewGuid().ToString();
            AppDomain domain     = AppDomain.CreateDomain(domainName, null, setup);

            try
            {
                LoadAssemblyHelper obj = (LoadAssemblyHelper)domain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location, "Uhuru.Utilities.LoadAssemblyHelper");

                CpuTarget target = obj.DetectPlatform(assemblyPath);

                return(target);
            }
            catch (System.BadImageFormatException)
            {
                return(CpuTarget.Unknown);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                AppDomain.Unload(domain);
            }
        }
        public static DotNetVersion GetVersion(string assemblyPath)
        {
            if (string.IsNullOrEmpty(assemblyPath))
            {
                throw new ArgumentException("Argument null or empty", "assemblyPath");
            }

            try
            {
                string fileName = assemblyPath.Normalize();

                if (!System.IO.File.Exists(fileName))
                {
                    return(DotNetVersion.Two);
                }

                // TODO: florind: find a safer way to do this, without loading the assembly in RAM
                AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
                setup.ApplicationBase = Path.GetDirectoryName(assemblyPath);
                string    domainName = Guid.NewGuid().ToString();
                AppDomain domain     = AppDomain.CreateDomain(domainName, null, setup);

                LoadAssemblyHelper obj = (LoadAssemblyHelper)domain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location, "Uhuru.Utilities.LoadAssemblyHelper");

                string version = obj.GetDotNetVersion(assemblyPath);

                AppDomain.Unload(domain);

                if (Convert.ToInt32(version, CultureInfo.InvariantCulture) < 4)
                {
                    return(DotNetVersion.Two);
                }
                else
                {
                    return(DotNetVersion.Four);
                }
            }
            catch (System.BadImageFormatException)
            {
                return(DotNetVersion.Two);
            }
            catch (Exception)
            {
                throw;
            }
        }