Example #1
0
    static IResourceReader GetReader(Stream stream, string name, bool useSourcePath)
    {
        string format = Path.GetExtension(name);

        switch (format.ToLower(System.Globalization.CultureInfo.InvariantCulture))
        {
        case ".po":
            return(new PoResourceReader(stream));

        case ".txt":
        case ".text":
            return(new TxtResourceReader(stream));

        case ".resources":
            return(new ResourceReader(stream));

        case ".resx":
            LoadResX();
            IResourceReader reader = (IResourceReader)Activator.CreateInstance(
                resxr, new object[] { stream });
            if (useSourcePath)               // only possible on 2.0 profile, or higher
            {
                PropertyInfo p = reader.GetType().GetProperty("BasePath",
                                                              BindingFlags.Public | BindingFlags.Instance);
                if (p != null && p.CanWrite)
                {
                    p.SetValue(reader, Path.GetDirectoryName(name), null);
                }
            }
            return(reader);

        default:
            throw new Exception("Unknown format in file " + name);
        }
    }
        internal RuntimeResourceSet(IResourceReader reader) :
            // explicitly do not call IResourceReader constructor since it caches all resources
            // the purpose of RuntimeResourceSet is to lazily load and cache.
            base()
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            _defaultReader = reader as DeserializingResourceReader ?? throw new ArgumentException(SR.Format(SR.NotSupported_WrongResourceReader_Type, reader.GetType()), nameof(reader));
            _resCache      = new Dictionary <string, ResourceLocator>(FastResourceComparer.Default);

            // in the CoreLib version RuntimeResourceSet creates ResourceReader and passes this in,
            // in the custom case ManifestBasedResourceReader creates the ResourceReader and passes it in
            // so we must initialize the cache here.
            _defaultReader._resCache = _resCache;
        }