Esempio n. 1
0
        // Namespace resolution
        //    if there is only one element with a name then no namespace is needed
        //    if there are multiple elements with a name
        //        namespace is required in order to match the correct one
        //    using declarations can provide implicit namespaces
        public static ProcessedType ResolveTagName(string tagName, string namespacePrefix, IReadOnlyList <string> namespaces)
        {
            FilterAssemblies();

            namespaces = namespaces ?? EmptyNamespaceList;

            if (string.IsNullOrEmpty(namespacePrefix))
            {
                namespacePrefix = null;
            }
            if (string.IsNullOrWhiteSpace(namespacePrefix))
            {
                namespacePrefix = null;
            }

            if (templateTypeMap.TryGetValue(tagName, out TypeList typeList))
            {
                // if this is null we resolve using just the tag name
                if (namespacePrefix == null)
                {
                    // if only one type has this tag name we can safely return it
                    if (typeList.types == null)
                    {
                        return(typeList.mainType.Reference());
                    }

                    // if there are multiple tags with this name, we need to search our namespaces
                    // if only one match is found, we can return it. If multiple are found, throw
                    // and ambiguous reference exception
                    LightList <ProcessedType> resultList = LightList <ProcessedType> .Get();

                    for (int i = 0; i < namespaces.Count; i++)
                    {
                        for (int j = 0; j < typeList.types.Length; j++)
                        {
                            string        namespaceName = namespaces[i];
                            ProcessedType testType      = typeList.types[j];
                            if (namespaceName == testType.namespaceName)
                            {
                                resultList.Add(testType);
                            }
                        }
                    }

                    if (resultList.size == 1)
                    {
                        ProcessedType retn = resultList[0];
                        resultList.Release();
                        return(retn.Reference());
                    }

                    List <string> list = resultList.Select((s) => s.namespaceName).ToList();
                    throw new ParseException("Ambiguous TagName reference: " + tagName + ". References found in namespaces " + StringUtil.ListToString(list, ", "));
                }

                if (typeList.types == null)
                {
                    if (namespacePrefix == typeList.mainType.namespaceName)
                    {
                        return(typeList.mainType.Reference());
                    }
                }
                else
                {
                    // if prefix is not null we can only return a match for that namespace
                    for (int j = 0; j < typeList.types.Length; j++)
                    {
                        ProcessedType testType = typeList.types[j];
                        if (namespacePrefix == testType.namespaceName)
                        {
                            return(testType.Reference());
                        }
                    }
                }

                return(null);
            }

            if (s_GenericMap.TryGetValue(tagName, out ProcessedType processedType))
            {
                return(processedType);
            }

            return(null);
        }