Exemple #1
0
        public static RuntimeAssemblyName Parse(String s)
        {
            Debug.Assert(s != null);
            AssemblyNameLexer lexer = new AssemblyNameLexer(s);

            // Name must come first.
            String name;

            AssemblyNameLexer.Token token = lexer.GetNext(out name);
            if (token != AssemblyNameLexer.Token.String)
            {
                if (token == AssemblyNameLexer.Token.End)
                {
                    throw new ArgumentException(SR.Format_StringZeroLength);
                }
                else
                {
                    throw new FileLoadException();
                }
            }

            if (name == String.Empty)
            {
                throw new FileLoadException();
            }

            Version version     = null;
            String  cultureName = null;

            byte[]            pkt   = null;
            AssemblyNameFlags flags = 0;

            LowLevelList <String> alreadySeen = new LowLevelList <String>();

            token = lexer.GetNext();
            while (token != AssemblyNameLexer.Token.End)
            {
                if (token != AssemblyNameLexer.Token.Comma)
                {
                    throw new FileLoadException();
                }
                String attributeName;

                token = lexer.GetNext(out attributeName);
                if (token != AssemblyNameLexer.Token.String)
                {
                    throw new FileLoadException();
                }
                token = lexer.GetNext();

                // Compat note: Inside AppX apps, the desktop CLR's AssemblyName parser skips past any elements that don't follow the "<Something>=<Something>" pattern.
                //  (when running classic Windows apps, such an illegal construction throws an exception as expected.)
                // Naturally, at least one app unwittingly takes advantage of this.
                if (token == AssemblyNameLexer.Token.Comma || token == AssemblyNameLexer.Token.End)
                {
                    continue;
                }

                if (token != AssemblyNameLexer.Token.Equals)
                {
                    throw new FileLoadException();
                }
                String attributeValue;
                token = lexer.GetNext(out attributeValue);
                if (token != AssemblyNameLexer.Token.String)
                {
                    throw new FileLoadException();
                }

                if (attributeName == String.Empty)
                {
                    throw new FileLoadException();
                }

                for (int i = 0; i < alreadySeen.Count; i++)
                {
                    if (alreadySeen[i].Equals(attributeName, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new FileLoadException(); // Cannot specify the same attribute twice.
                    }
                }
                alreadySeen.Add(attributeName);

                if (attributeName.Equals("Version", StringComparison.OrdinalIgnoreCase))
                {
                    version = ParseVersion(attributeValue);
                }

                if (attributeName.Equals("Culture", StringComparison.OrdinalIgnoreCase))
                {
                    cultureName = ParseCulture(attributeValue);
                }

                if (attributeName.Equals("PublicKeyToken", StringComparison.OrdinalIgnoreCase))
                {
                    pkt = ParsePKT(attributeValue);
                }

                if (attributeName.Equals("ProcessorArchitecture", StringComparison.OrdinalIgnoreCase))
                {
                    flags |= (AssemblyNameFlags)(((int)ParseProcessorArchitecture(attributeValue)) << 4);
                }

                if (attributeName.Equals("Retargetable", StringComparison.OrdinalIgnoreCase))
                {
                    if (attributeValue.Equals("Yes", StringComparison.OrdinalIgnoreCase))
                    {
                        flags |= AssemblyNameFlags.Retargetable;
                    }
                    else if (attributeValue.Equals("No", StringComparison.OrdinalIgnoreCase))
                    {
                        // nothing to do
                    }
                    else
                    {
                        throw new FileLoadException();
                    }
                }

                if (attributeName.Equals("ContentType", StringComparison.OrdinalIgnoreCase))
                {
                    if (attributeValue.Equals("WindowsRuntime", StringComparison.OrdinalIgnoreCase))
                    {
                        flags |= (AssemblyNameFlags)(((int)AssemblyContentType.WindowsRuntime) << 9);
                    }
                    else
                    {
                        throw new FileLoadException();
                    }
                }

                // Desktop compat: If we got here, the attribute name is unknown to us. Ignore it (as long it's not duplicated.)
                token = lexer.GetNext();
            }
            return(new RuntimeAssemblyName(name, version, cultureName, flags, pkt));
        }
        internal static RuntimeAssemblyName Parse(String s)
        {
            Debug.Assert(s != null);
            AssemblyNameLexer lexer = new AssemblyNameLexer(s);

            // Name must come first.
            String name;
            AssemblyNameLexer.Token token = lexer.GetNext(out name);
            if (token != AssemblyNameLexer.Token.String)
            {
                if (token == AssemblyNameLexer.Token.End)
                    throw new ArgumentException(SR.Format_StringZeroLength);
                else
                    throw new FileLoadException();
            }

            if (name == String.Empty)
                throw new FileLoadException();

            Version version = null;
            String cultureName = null;
            byte[] pkt = null;
            AssemblyNameFlags flags = 0;

            LowLevelList<String> alreadySeen = new LowLevelList<String>();
            token = lexer.GetNext();
            while (token != AssemblyNameLexer.Token.End)
            {
                if (token != AssemblyNameLexer.Token.Comma)
                    throw new FileLoadException();
                String attributeName;

                token = lexer.GetNext(out attributeName);
                if (token != AssemblyNameLexer.Token.String)
                    throw new FileLoadException();
                token = lexer.GetNext();

                // Compat note: Inside AppX apps, the desktop CLR's AssemblyName parser skips past any elements that don't follow the "<Something>=<Something>" pattern.
                //  (when running classic Windows apps, such an illegal construction throws an exception as expected.)
                // Naturally, at least one app unwittingly takes advantage of this.
                if (token == AssemblyNameLexer.Token.Comma || token == AssemblyNameLexer.Token.End)
                    continue;

                if (token != AssemblyNameLexer.Token.Equals)
                    throw new FileLoadException();
                String attributeValue;
                token = lexer.GetNext(out attributeValue);
                if (token != AssemblyNameLexer.Token.String)
                    throw new FileLoadException();

                if (attributeName == String.Empty)
                    throw new FileLoadException();

                for (int i = 0; i < alreadySeen.Count; i++)
                {
                    if (alreadySeen[i].Equals(attributeName, StringComparison.OrdinalIgnoreCase))
                        throw new FileLoadException(); // Cannot specify the same attribute twice.
                }
                alreadySeen.Add(attributeName);

                if (attributeName.Equals("Version", StringComparison.OrdinalIgnoreCase))
                {
                    version = ParseVersion(attributeValue);
                }

                if (attributeName.Equals("Culture", StringComparison.OrdinalIgnoreCase))
                {
                    cultureName = ParseCulture(attributeValue);
                }

                if (attributeName.Equals("PublicKeyToken", StringComparison.OrdinalIgnoreCase))
                {
                    pkt = ParsePKT(attributeValue);
                }

                if (attributeName.Equals("ProcessorArchitecture", StringComparison.OrdinalIgnoreCase))
                {
                    flags |= (AssemblyNameFlags)(((int)ParseProcessorArchitecture(attributeValue)) << 4);
                }

                if (attributeName.Equals("Retargetable", StringComparison.OrdinalIgnoreCase))
                {
                    if (attributeValue.Equals("Yes", StringComparison.OrdinalIgnoreCase))
                        flags |= AssemblyNameFlags.Retargetable;
                    else if (attributeValue.Equals("No", StringComparison.OrdinalIgnoreCase))
                    {
                        // nothing to do
                    }
                    else
                        throw new FileLoadException();
                }

                if (attributeName.Equals("ContentType", StringComparison.OrdinalIgnoreCase))
                {
                    if (attributeValue.Equals("WindowsRuntime", StringComparison.OrdinalIgnoreCase))
                        flags |= (AssemblyNameFlags)(((int)AssemblyContentType.WindowsRuntime) << 9);
                    else
                        throw new FileLoadException();
                }

                // Desktop compat: If we got here, the attribute name is unknown to us. Ignore it (as long it's not duplicated.)
                token = lexer.GetNext();
            }
            return new RuntimeAssemblyName(name, version, cultureName, flags, pkt);
        }