private bool CanUseDefaultResourceClasses(string readerTypeName, string resSetTypeName)
        {
            Debug.Assert(readerTypeName != null, "readerTypeName shouldn't be null; check caller");
            Debug.Assert(resSetTypeName != null, "resSetTypeName shouldn't be null; check caller");

            if (_mediator.UserResourceSet != null)
            {
                return(false);
            }

            // Ignore the actual version of the ResourceReader and
            // RuntimeResourceSet classes.  Let those classes deal with
            // versioning themselves.

            if (readerTypeName != null)
            {
                if (!ResourceManager.IsDefaultType(readerTypeName, ResourceManager.ResReaderTypeName))
                {
                    return(false);
                }
            }

            if (resSetTypeName != null)
            {
                if (!ResourceManager.IsDefaultType(resSetTypeName, ResourceManager.ResSetTypeName))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #2
0
        private static ResourceSet InternalGetResourceSetFromSerializedData(Stream store, string readerTypeName, string?resSetTypeName, ResourceManager.ResourceManagerMediator mediator)
        {
            IResourceReader reader;

            // Permit deserialization as long as the default ResourceReader is used
            if (ResourceManager.IsDefaultType(readerTypeName, ResourceManager.ResReaderTypeName))
            {
                reader = new ResourceReader(
                    store,
                    new Dictionary <string, ResourceLocator>(FastResourceComparer.Default),
                    permitDeserialization: true);
            }
            else
            {
                Type     readerType = Type.GetType(readerTypeName, throwOnError: true) !;
                object[] args       = new object[1];
                args[0] = store;
                reader  = (IResourceReader)Activator.CreateInstance(readerType, args) !;
            }

            object[] resourceSetArgs = new object[1];
            resourceSetArgs[0] = reader;

            Type?resSetType = mediator.UserResourceSet;

            if (resSetType == null)
            {
                Debug.Assert(resSetTypeName != null, "We should have a ResourceSet type name from the custom resource file here.");
                resSetType = Type.GetType(resSetTypeName, true, false) !;
            }

            ResourceSet rs = (ResourceSet)Activator.CreateInstance(resSetType,
                                                                   BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance,
                                                                   null,
                                                                   resourceSetArgs,
                                                                   null,
                                                                   null) !;

            return(rs);
        }
        internal ResourceSet CreateResourceSet(Stream store, Assembly assembly)
        {
            Debug.Assert(store != null, "I need a Stream!");
            // Check to see if this is a Stream the ResourceManager understands,
            // and check for the correct resource reader type.
            if (store.CanSeek && store.Length > 4)
            {
                long startPos = store.Position;

                // not disposing because we want to leave stream open
                BinaryReader br = new BinaryReader(store);

                // Look for our magic number as a little endian int.
                int bytes = br.ReadInt32();
                if (bytes == ResourceManager.MagicNumber)
                {
                    int    resMgrHeaderVersion = br.ReadInt32();
                    string?readerTypeName = null, resSetTypeName = null;
                    if (resMgrHeaderVersion == ResourceManager.HeaderVersionNumber)
                    {
                        br.ReadInt32();  // We don't want the number of bytes to skip.
                        readerTypeName = br.ReadString();
                        resSetTypeName = br.ReadString();
                    }
                    else if (resMgrHeaderVersion > ResourceManager.HeaderVersionNumber)
                    {
                        // Assume that the future ResourceManager headers will
                        // have two strings for us - the reader type name and
                        // resource set type name.  Read those, then use the num
                        // bytes to skip field to correct our position.
                        int  numBytesToSkip = br.ReadInt32();
                        long endPosition    = br.BaseStream.Position + numBytesToSkip;

                        readerTypeName = br.ReadString();
                        resSetTypeName = br.ReadString();

                        br.BaseStream.Seek(endPosition, SeekOrigin.Begin);
                    }
                    else
                    {
                        // resMgrHeaderVersion is older than this ResMgr version.
                        // We should add in backwards compatibility support here.
                        Debug.Assert(_mediator.MainAssembly != null);
                        throw new NotSupportedException(SR.Format(SR.NotSupported_ObsoleteResourcesFile, _mediator.MainAssembly.GetName().Name));
                    }

                    store.Position = startPos;
                    // Perf optimization - Don't use Reflection for our defaults.
                    // Note there are two different sets of strings here - the
                    // assembly qualified strings emitted by ResourceWriter, and
                    // the abbreviated ones emitted by InternalResGen.
                    if (CanUseDefaultResourceClasses(readerTypeName, resSetTypeName))
                    {
                        return(new RuntimeResourceSet(store, permitDeserialization: true));
                    }
                    else
                    {
                        IResourceReader reader;

                        // Permit deserialization as long as the default ResourceReader is used
                        if (ResourceManager.IsDefaultType(readerTypeName, ResourceManager.ResReaderTypeName))
                        {
                            reader = new ResourceReader(
                                store,
                                new Dictionary <string, ResourceLocator>(FastResourceComparer.Default),
                                permitDeserialization: true);
                        }
                        else
                        {
                            Type     readerType = Type.GetType(readerTypeName, throwOnError: true) !;
                            object[] args       = new object[1];
                            args[0] = store;
                            reader  = (IResourceReader)Activator.CreateInstance(readerType, args) !;
                        }

                        object[] resourceSetArgs = new object[1];
                        resourceSetArgs[0] = reader;

                        Type resSetType;
                        if (_mediator.UserResourceSet == null)
                        {
                            Debug.Assert(resSetTypeName != null, "We should have a ResourceSet type name from the custom resource file here.");
                            resSetType = Type.GetType(resSetTypeName, true, false) !;
                        }
                        else
                        {
                            resSetType = _mediator.UserResourceSet;
                        }

                        ResourceSet rs = (ResourceSet)Activator.CreateInstance(resSetType,
                                                                               BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance,
                                                                               null,
                                                                               resourceSetArgs,
                                                                               null,
                                                                               null) !;
                        return(rs);
                    }
                }
                else
                {
                    store.Position = startPos;
                }
            }

            if (_mediator.UserResourceSet == null)
            {
                return(new RuntimeResourceSet(store, permitDeserialization: true));
            }
            else
            {
                object[] args = new object[2];
                args[0] = store;
                args[1] = assembly;
                try
                {
                    ResourceSet?rs = null;
                    // Add in a check for a constructor taking in an assembly first.
                    try
                    {
                        rs = (ResourceSet)Activator.CreateInstance(_mediator.UserResourceSet, args) !;
                        return(rs);
                    }
                    catch (MissingMethodException) { }

                    args    = new object[1];
                    args[0] = store;
                    rs      = (ResourceSet)Activator.CreateInstance(_mediator.UserResourceSet, args) !;

                    return(rs);
                }
                catch (MissingMethodException e)
                {
                    throw new InvalidOperationException(SR.Format(SR.InvalidOperation_ResMgrBadResSet_Type, _mediator.UserResourceSet.AssemblyQualifiedName), e);
                }
            }
        }
 private static bool ValidateReaderType(string readerType)
 {
     return(ResourceManager.IsDefaultType(readerType, ResourceManager.ResReaderTypeName));
 }