Exemplo n.º 1
0
        CodeTypeReference GetCustomNamespaceUrlType(XmlType xmlType)
        {
            if (_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes();
            }

            IList <XamlLoader.FallbackTypeInfo> potentialTypes;
            TypeReference typeReference = xmlType.GetTypeReference <TypeReference>(
                _xmlnsDefinitions,
                null,
                (typeInfo) =>
            {
                ModuleDefinition module = null;
                if (!_xmlnsModules.TryGetValue(typeInfo.AssemblyName, out module))
                {
                    return(null);
                }
                string typeName = typeInfo.TypeName.Replace('+', '/');                         //Nested types
                string fullName = $"{typeInfo.ClrNamespace}.{typeInfo.TypeName}";
                return(module.Types.Where(t => t.FullName == fullName).FirstOrDefault());
            },
                out potentialTypes);

            if (typeReference == null)
            {
                throw new Exception($"Type {xmlType.Name} not found in xmlns {xmlType.NamespaceUri}");
            }

            return(new CodeTypeReference(typeReference.FullName));
        }
Exemplo n.º 2
0
        public static bool TryGetTypeReference(this XmlType xmlType, ModuleDefinition module, IXmlLineInfo xmlInfo, out TypeReference typeReference)
        {
            IList <XmlnsDefinitionAttribute> xmlnsDefinitions = null;

            lock (_nsLock) {
                if (!s_xmlnsDefinitions.TryGetValue(module, out xmlnsDefinitions))
                {
                    xmlnsDefinitions = GatherXmlnsDefinitionAttributes(module);
                }
            }

            var typeArguments = xmlType.TypeArguments;

            TypeReference type = xmlType.GetTypeReference(xmlnsDefinitions, module.Assembly.Name.Name, (typeInfo) => {
                string typeName = typeInfo.TypeName.Replace('+', '/');                         //Nested types
                return(module.GetTypeDefinition((typeInfo.AssemblyName, typeInfo.ClrNamespace, typeName)));
            },
Exemplo n.º 3
0
            public INode Parse(string match, ref string remaining, IServiceProvider serviceProvider)
            {
                var nsResolver = serviceProvider.GetService(typeof(IXmlNamespaceResolver)) as IXmlNamespaceResolver;

                if (nsResolver == null)
                {
                    throw new ArgumentException();
                }
                IXmlLineInfo xmlLineInfo         = null;
                var          xmlLineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;

                if (xmlLineInfoProvider != null)
                {
                    xmlLineInfo = xmlLineInfoProvider.XmlLineInfo;
                }
                var contextProvider = serviceProvider.GetService(typeof(ILContextProvider)) as ILContextProvider;

                var split = match.Split(':');

                if (split.Length > 2)
                {
                    throw new ArgumentException();
                }

                string prefix, name;

                if (split.Length == 2)
                {
                    prefix = split[0];
                    name   = split[1];
                }
                else
                {
                    prefix = "";
                    name   = split[0];
                }

                var namespaceuri = nsResolver.LookupNamespace(prefix) ?? "";

                if (!string.IsNullOrEmpty(prefix) && string.IsNullOrEmpty(namespaceuri))
                {
                    throw new XamlParseException($"Undeclared xmlns prefix '{prefix}'", xmlLineInfo);
                }
                //The order of lookup is to look for the Extension-suffixed class name first and then look for the class name without the Extension suffix.
                XmlType type;

                try
                {
                    type = new XmlType(namespaceuri, name + "Extension", null);
                    type.GetTypeReference(contextProvider.Context.Module, null);
                }
                catch (XamlParseException)
                {
                    type = new XmlType(namespaceuri, name, null);
                }

                if (type == null)
                {
                    throw new NotSupportedException();
                }

                node = xmlLineInfo == null
                                        ? new ElementNode(type, "", nsResolver)
                                        : new ElementNode(type, "", nsResolver, xmlLineInfo.LineNumber, xmlLineInfo.LinePosition);

                if (remaining.StartsWith("}", StringComparison.Ordinal))
                {
                    remaining = remaining.Substring(1);
                    return(node);
                }

                char   next;
                string piece;

                while ((piece = GetNextPiece(ref remaining, out next)) != null)
                {
                    HandleProperty(piece, serviceProvider, ref remaining, next != '=');
                }

                return(node);
            }