Esempio n. 1
0
        static IList <string> PrefixesToIgnore(IList <KeyValuePair <string, string> > xmlns)
        {
            var prefixes = new List <string>();

            foreach (var kvp in xmlns)
            {
                var prefix = kvp.Key;

                string typeName = null, ns = null, asm = null, targetPlatform = null;
                XmlnsHelper.ParseXmlns(kvp.Value, out typeName, out ns, out asm, out targetPlatform);
                if (targetPlatform == null)
                {
                    continue;
                }
                try {
                    if (targetPlatform != Device.RuntimePlatform)
                    {
                        // Special case for Windows backward compatibility
                        if (targetPlatform == "Windows" && Device.RuntimePlatform == Device.UWP)
                        {
                            continue;
                        }

                        prefixes.Add(prefix);
                    }
                } catch (InvalidOperationException) {
                    prefixes.Add(prefix);
                }
            }
            return(prefixes);
        }
Esempio n. 2
0
        static IList <string> PrefixesToIgnore(IList <KeyValuePair <string, string> > xmlns)
        {
            var prefixes = new List <string>();

            foreach (var kvp in xmlns)
            {
                var prefix = kvp.Key;

                string typeName = null, ns = null, asm = null, targetPlatform = null;
                XmlnsHelper.ParseXmlns(kvp.Value, out typeName, out ns, out asm, out targetPlatform);
                if (targetPlatform == null)
                {
                    continue;
                }
                TargetPlatform os;
                if (Enum.TryParse <TargetPlatform>(targetPlatform, out os) && os != Device.OS)
                {
                    prefixes.Add(prefix);
                }
            }
            return(prefixes);
        }
Esempio n. 3
0
        public static T GetTypeReference <T>(
            this XmlType xmlType,
            IEnumerable <XmlnsDefinitionAttribute> xmlnsDefinitions,
            string defaultAssemblyName,
            Func <XamlLoader.FallbackTypeInfo, T> refFromTypeInfo,
            out IList <XamlLoader.FallbackTypeInfo> potentialTypes)
            where T : class
        {
            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var namespaceURI     = xmlType.NamespaceUri;
            var elementName      = xmlType.Name;
            var typeArguments    = xmlType.TypeArguments;

            potentialTypes = null;

            foreach (var xmlnsDef in xmlnsDefinitions)
            {
                if (xmlnsDef.XmlNamespace != namespaceURI)
                {
                    continue;
                }
                lookupAssemblies.Add(xmlnsDef);
            }

            if (lookupAssemblies.Count == 0)
            {
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out var ns, out var asmstring, out _);
                asmstring = asmstring ?? defaultAssemblyName;
                if (namespaceURI != null && ns != null)
                {
                    lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns)
                    {
                        AssemblyName = asmstring
                    });
                }
            }

            var lookupNames = new List <string>();

            if (elementName != "DataTemplate" && !elementName.EndsWith("Extension", StringComparison.Ordinal))
            {
                lookupNames.Add(elementName + "Extension");
            }
            lookupNames.Add(elementName);

            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count;                     //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            potentialTypes = new List <XamlLoader.FallbackTypeInfo>();
            foreach (string typeName in lookupNames)
            {
                foreach (XmlnsDefinitionAttribute xmlnsDefinitionAttribute in lookupAssemblies)
                {
                    potentialTypes.Add(new XamlLoader.FallbackTypeInfo {
                        ClrNamespace = xmlnsDefinitionAttribute.ClrNamespace,
                        TypeName     = typeName,
                        AssemblyName = xmlnsDefinitionAttribute.AssemblyName,
                        XmlNamespace = xmlnsDefinitionAttribute.XmlNamespace
                    });
                }
            }

            T type = null;

            foreach (XamlLoader.FallbackTypeInfo typeInfo in potentialTypes)
            {
                if ((type = refFromTypeInfo(typeInfo)) != null)
                {
                    break;
                }
            }

            return(type);
        }
Esempio n. 4
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            if (s_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes();
            }

            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var lookupNames      = new List <string>();

            foreach (var xmlnsDef in s_xmlnsDefinitions)
            {
                if (xmlnsDef.XmlNamespace != namespaceURI)
                {
                    continue;
                }
                lookupAssemblies.Add(xmlnsDef);
            }

            if (lookupAssemblies.Count == 0)
            {
                string ns, asmstring, _;
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out ns, out asmstring, out _);
                lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns)
                {
                    AssemblyName = asmstring ?? currentAssembly.FullName
                });
            }

            lookupNames.Add(elementName);
            lookupNames.Add(elementName + "Extension");

            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count;                     //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                foreach (var name in lookupNames)
                {
                    if ((type = Type.GetType($"{asm.ClrNamespace}.{name}, {asm.AssemblyName}")) != null)
                    {
                        break;
                    }
                }
                if (type != null)
                {
                    break;
                }
            }

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

            if (type == null)
            {
                exception = new XamlParseException($"Type {elementName} not found in xmlns {namespaceURI}", xmlInfo);
            }

            return(type);
        }
Esempio n. 5
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            var lookupAssemblies = new List <Tuple <string, string> >();          //namespace, assemblyqualifiednamed
            var lookupNames      = new List <string>();

            if (!XmlnsHelper.IsCustom(namespaceURI))
            {
                lookupAssemblies.Add(new Tuple <string, string>("Xamarin.Forms", typeof(View).GetTypeInfo().Assembly.FullName));
                lookupAssemblies.Add(new Tuple <string, string>("Xamarin.Forms.Xaml", typeof(XamlLoader).GetTypeInfo().Assembly.FullName));
            }
            else if (namespaceURI == "http://schemas.microsoft.com/winfx/2009/xaml" ||
                     namespaceURI == "http://schemas.microsoft.com/winfx/2006/xaml")
            {
                lookupAssemblies.Add(new Tuple <string, string>("Xamarin.Forms.Xaml", typeof(XamlLoader).GetTypeInfo().Assembly.FullName));
                lookupAssemblies.Add(new Tuple <string, string>("System", typeof(object).GetTypeInfo().Assembly.FullName));              //mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
                lookupAssemblies.Add(new Tuple <string, string>("System", typeof(Uri).GetTypeInfo().Assembly.FullName));                 //System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
            }
            else
            {
                string ns, asmstring, _;
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out ns, out asmstring, out _);
                lookupAssemblies.Add(new Tuple <string, string>(ns, asmstring ?? currentAssembly.FullName));
            }

            lookupNames.Add(elementName);
            if (namespaceURI == "http://schemas.microsoft.com/winfx/2009/xaml")
            {
                lookupNames.Add(elementName + "Extension");
            }
            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count;                     //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                foreach (var name in lookupNames)
                {
                    if ((type = Type.GetType($"{asm.Item1}.{name}, {asm.Item2}")) != null)
                    {
                        break;
                    }
                }
                if (type != null)
                {
                    break;
                }
            }

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

            if (type == null)
            {
                exception = new XamlParseException($"Type {elementName} not found in xmlns {namespaceURI}", xmlInfo);
            }

            return(type);
        }
Esempio n. 6
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            List <Tuple <string, Assembly> > lookupAssemblies = new List <Tuple <string, Assembly> >();
            List <string> lookupNames = new List <string>();

            if (!XmlnsHelper.IsCustom(namespaceURI))
            {
                lookupAssemblies.Add(new Tuple <string, Assembly>("Xamarin.Forms", typeof(View).GetTypeInfo().Assembly));
                lookupAssemblies.Add(new Tuple <string, Assembly>("Xamarin.Forms.Xaml", typeof(XamlLoader).GetTypeInfo().Assembly));
            }
            else if (namespaceURI == "http://schemas.microsoft.com/winfx/2009/xaml" ||
                     namespaceURI == "http://schemas.microsoft.com/winfx/2006/xaml")
            {
                lookupAssemblies.Add(new Tuple <string, Assembly>("Xamarin.Forms.Xaml", typeof(XamlLoader).GetTypeInfo().Assembly));
                lookupAssemblies.Add(new Tuple <string, Assembly>("System", typeof(object).GetTypeInfo().Assembly));
                lookupAssemblies.Add(new Tuple <string, Assembly>("System", typeof(Uri).GetTypeInfo().Assembly));                 //System.dll
            }
            else
            {
                string   ns;
                string   typename;
                string   asmstring;
                Assembly asm;

                XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring);
                asm = asmstring == null ? currentAssembly : Assembly.Load(new AssemblyName(asmstring));
                lookupAssemblies.Add(new Tuple <string, Assembly>(ns, asm));
            }

            lookupNames.Add(elementName);
            if (namespaceURI == "http://schemas.microsoft.com/winfx/2009/xaml")
            {
                lookupNames.Add(elementName + "Extension");
            }
            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count;                     //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                if (type != null)
                {
                    break;
                }
                foreach (var name in lookupNames)
                {
                    if (type != null)
                    {
                        break;
                    }
                    type = asm.Item2.GetType(asm.Item1 + "." + name);
                }
            }

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

            if (type == null)
            {
                exception = new XamlParseException(string.Format("Type {0} not found in xmlns {1}", elementName, namespaceURI),
                                                   xmlInfo);
                return(null);
            }

            return(type);
        }
Esempio n. 7
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            bool hasRetriedNsSearch = false;

retry:
            if (s_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes();
            }

            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var lookupNames      = new List <string>();

            foreach (var xmlnsDef in s_xmlnsDefinitions)
            {
                if (xmlnsDef.XmlNamespace != namespaceURI)
                {
                    continue;
                }
                lookupAssemblies.Add(xmlnsDef);
            }

            if (lookupAssemblies.Count == 0)
            {
                string ns, asmstring, _;
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out ns, out asmstring, out _);
                lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns)
                {
                    AssemblyName = asmstring ?? currentAssembly.FullName
                });
            }

            lookupNames.Add(elementName);
            lookupNames.Add(elementName + "Extension");

            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count;                     //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                foreach (var name in lookupNames)
                {
                    if ((type = Type.GetType($"{asm.ClrNamespace}.{name}, {asm.AssemblyName}")) != null)
                    {
                        break;
                    }
                }
                if (type != null)
                {
                    break;
                }
            }

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

            if (type == null)
            {
#if NETSTANDARD2_0
                // 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
                exception = new XamlParseException($"Type {elementName} not found in xmlns {namespaceURI}. Ensure third party control libraries are referenced in the code of your project and not just in XAML.  Third party control authors must also apply the \"Preserve\" attribute on their assemblies or any control classes.", xmlInfo);
            }

            return(type);
        }