bool TryGetSatelliteAssembly(CultureInfo culture, Assembly mainAssembly, ref Assembly satelliteAssembly)
 {
     try
     {
         satelliteAssembly = mainAssembly.GetSatelliteAssembly(culture);
         return true;
     }
     catch (Exception)
     {
         return false;
     }
 }
 public static Stream GetLocalizedManifestResourceStream(string manifestName, Assembly baseAssembly, CultureInfo culture)
 {
     if ((manifestName == null) || (manifestName.Length == 0))
     {
         throw new ArgumentNullException("manifestName");
     }
     if (baseAssembly == null)
     {
         throw new ArgumentNullException("baseAssembly");
     }
     if (culture == null)
     {
         throw new ArgumentNullException("culture");
     }
     Stream stream = baseAssembly.GetSatelliteAssembly(culture).GetManifestResourceStream(manifestName);
     if ((stream == null) && manifestName.Contains("_"))
     {
         stream = baseAssembly.GetSatelliteAssembly(culture).GetManifestResourceStream(manifestName.Replace('_', ' '));
     }
     return stream;
 }
 public static Stream GetLocalizedManifestResourceStreamIfExists(string manifestName, Assembly baseAssembly, CultureInfo culture)
 {
     if ((manifestName == null) || (manifestName.Length == 0))
     {
         throw new ArgumentNullException("manifestName");
     }
     if (baseAssembly == null)
     {
         throw new ArgumentNullException("baseAssembly");
     }
     if (culture == null)
     {
         throw new ArgumentNullException("culture");
     }
     try
     {
         return baseAssembly.GetSatelliteAssembly(culture).GetManifestResourceStream(manifestName);
     }
     catch (FileNotFoundException)
     {
         return null;
     }
 }
        public TranslationBundle LoadSatellite(Assembly parentAssembly = null, Version assemblyVersion = null)
        {
            try
            {
                parentAssembly = parentAssembly ?? Assembly.GetEntryAssembly();
                var parentAssemblyName = new AssemblyName(parentAssembly.FullName);

                var assembly = parentAssembly.GetSatelliteAssembly(Culture, assemblyVersion);
                var resourceNames = assembly.GetManifestResourceNames();
                if (resourceNames.Length != 1)
                    throw new IOException("Satellite resource must contain exactly one manifest resource.");
                var resourceName = string.Format("{0}.{1}.{2}", parentAssemblyName.Name, ResourceNamespace, Culture.Name);

                var resourceManager = new ResourceManager(resourceName, assembly);
                var obj = resourceManager.GetObject(ResourceObjectName);
                if (!(obj is byte[])) throw new IOException("Invalid satellite resource object type.");
                var stream = new MemoryStream((byte[])obj);

                using (stream) Load(stream);
            }
            catch
            {
                if (!Silent) throw;
            }

            return (this);
        }
Beispiel #5
0
        public ExceptionVerifier(string assemblyName, ExceptionVerificationFlags flags, ITestOutputHelper output)
        {
            _output = output;

            if (assemblyName == null)
                throw new VerifyException("Assembly name cannot be null");

            _verificationFlags = flags;

            try
            {
                switch (assemblyName.ToUpper())
                {
                    case "SYSTEM.XML":
                        {
                            var dom = new XmlDocument();
                            _asm = dom.GetType().GetTypeInfo().Assembly;
                        }
                        break;
                    //case "SYSTEM.DATA":
                    //{
                    //    var ds = new DataSet();
                    //    asm = ds.GetType().Assembly;
                    //}
                    //    break;
                    default:
                        throw new FileLoadException("Cannot load assembly from " + GetRuntimeInstallDir() + assemblyName + ".dll");
                        //asm = Assembly.LoadFrom(GetRuntimeInstallDir() + assemblyName + ".dll");
                        //break;
                }

                if (_asm == null)
                    throw new VerifyException("Can not load assembly " + assemblyName);

                // let's determine if this is a loc run, if it is then we need to load satellite assembly
                _locAsm = null;
                if (!CultureInfo.CurrentCulture.Equals(new CultureInfo("en-US")) && !CultureInfo.CurrentCulture.Equals(new CultureInfo("en")))
                {
                    try
                    {
                        // load satellite assembly
                        _locAsm = _asm.GetSatelliteAssembly(new CultureInfo(CultureInfo.CurrentCulture.Parent.IetfLanguageTag));
                    }
                    catch (FileNotFoundException e1)
                    {
                        _output.WriteLine(e1.ToString());
                    }
                    catch (FileLoadException e2)
                    {
                        _output.WriteLine(e2.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                _output.WriteLine("Exception: " + e.Message);
                _output.WriteLine("Stack: " + e.StackTrace);
                throw new VerifyException("Error while loading assembly");
            }

            string[] resArray;
            Stream resStream = null;
            var bFound = false;

            // Check that assembly manifest has resources
            if (null != _locAsm)
                resArray = _locAsm.GetManifestResourceNames();
            else
                resArray = _asm.GetManifestResourceNames();

            foreach (var s in resArray)
            {
                if (s.EndsWith(".resources"))
                {
                    resStream = null != _locAsm ? _locAsm.GetManifestResourceStream(s) : _asm.GetManifestResourceStream(s);
                    bFound = true;
                    if (bFound && resStream != null)
                    {
                        // Populate hashtable from resources
                        var resReader = new ResourceReader(resStream);
                        if (_resources == null)
                        {
                            _resources = new Hashtable();
                        }
                        var ide = resReader.GetEnumerator();
                        while (ide.MoveNext())
                        {
                            if (!_resources.ContainsKey(ide.Key.ToString()))
                                _resources.Add(ide.Key.ToString(), ide.Value.ToString());
                        }
                        resReader.Dispose();
                    }
                    //break;
                }
            }

            if (!bFound || resStream == null)
                throw new VerifyException("GetManifestResourceStream() failed");
        }
        /// <summary>
        /// Adds the given assembly to the included set, if and only
        /// if it is not contained by the encountered set and it
        /// is not a system assembly; a core HSQLDB assembly; an
        /// IKVM assembly; an NUnit assembly or a TestDriven.NET
        /// assembly.
        /// </summary>
        /// <remarks>
        /// The exclusion list could be made much larger to produce
        /// a higher quality included set, but at what price?
        /// </remarks>
        /// <param name="encountered">The assemblies encountered so far.</param>
        /// <param name="included">The assemblies included so far.</param>
        /// <param name="entry">The assembly to add.</param>
        internal static void AddAssembly(
            Set encountered,
            Set included,
            Assembly entry)
        {
            if (entry == null || encountered.contains(entry))
            {
                return;
            }
            else
            {
                encountered.add(entry);
            }

            string simpleName = entry.GetName().Name;

            // ignored (performance optimization)
            if (simpleName == "System"
                || simpleName == "mscorlib"
                || simpleName == "Org.Hsqldb"
                || simpleName.StartsWith("System.")
                || simpleName.StartsWith("IKVM.")
                || simpleName.StartsWith("nunit.")
                || simpleName.StartsWith("TestDriven."))
            {
                return;
            }
            else
            {
                included.add(entry);
            }

            try
            {
                Assembly satellite =
                    entry.GetSatelliteAssembly(CultureInfo.CurrentCulture);

                IkvmResourceLoaderFactory.AddAssembly(
                    encountered,
                    included,
                    satellite);
            }
            catch (Exception ex)
            {
            #if DEBUG
                Debug.WriteLine(ex);
            #endif
            }

            AssemblyName[] referencedAssemblies
                = entry.GetReferencedAssemblies();

            for (int i = 0; i < referencedAssemblies.Length; i++)
            {
                AssemblyName assemblyName = referencedAssemblies[i];

                try
                {
                    Assembly referencedAssembly
                        = Assembly.Load(assemblyName);

                    IkvmResourceLoaderFactory.AddAssembly(
                        encountered,
                        included,
                        referencedAssembly);
                }
                catch (System.Exception ex)
                {
            #if DEBUG
                    Debug.WriteLine(ex);
            #endif
                }
            }
        }