コード例 #1
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
#if NETSTANDARD2_0 || NET6_0
            bool hasRetriedNsSearch = false;
#endif
            IList <XamlLoader.FallbackTypeInfo> potentialTypes;

#if NETSTANDARD2_0 || NET6_0
retry:
#endif
            if (s_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes();
            }

            Type type = xmlType.GetTypeReference(
                s_xmlnsDefinitions,
                currentAssembly?.FullName,
                (typeInfo) =>
                Type.GetType($"{typeInfo.ClrNamespace}.{typeInfo.TypeName}, {typeInfo.AssemblyName}"),
                out potentialTypes);

            var typeArguments = xmlType.TypeArguments;
            exception = null;

#if NETSTANDARD2_0 || NET6_0
            if (type == null)
            {
                // This covers the scenario where the AppDomain's loaded
                // assemblies might have changed since this method was first
                // called. This occurred during unit test runs and could
                // conceivably occur in the field.
                if (!hasRetriedNsSearch)
                {
                    hasRetriedNsSearch = true;
                    s_xmlnsDefinitions = null;
                    goto retry;
                }
            }
#endif

            if (XamlLoader.FallbackTypeResolver != null)
            {
                type = XamlLoader.FallbackTypeResolver(potentialTypes, type);
            }

            if (type != null && typeArguments != null)
            {
                XamlParseException innerexception = null;
                var args = typeArguments.Select(delegate(XmlType xmltype)
                {
                    var t = GetElementType(xmltype, xmlInfo, currentAssembly, out XamlParseException xpe);
                    if (xpe != null)
                    {
                        innerexception = xpe;
                        return(null);
                    }
                    return(t);
                }).ToArray();
                if (innerexception != null)
                {
                    exception = innerexception;
                    return(null);
                }

                try
                {
                    type = type.MakeGenericType(args);
                }
                catch (InvalidOperationException)
                {
                    exception = new XamlParseException($"Type {type} is not a GenericTypeDefinition", xmlInfo);
                }
            }

            if (type == null)
            {
                exception = new XamlParseException($"Type {xmlType.Name} not found in xmlns {xmlType.NamespaceUri}", xmlInfo);
            }

            return(type);
        }