Beispiel #1
0
        ///<summary>
        /// Returns a NamespaceMapEntry array given a xmlNamespace.  Each
        /// NamespaceMapEntry contains information the XamlTypeMapper uses for Mapping between an xml
        /// XmlNamespace and what Assembly, Namespace to look in.
        ///</summary>
        ///<param name="xmlNamespace">
        /// The xmlNamespace for the Map
        ///</param>
        ///<returns>
        /// Returns an Array of NamespaceMapEntry
        ///</returns>
        internal NamespaceMapEntry[] GetNamespaceMapEntries(string xmlNamespace)
        {
            NamespaceMapEntry[] namespaceMaps = null;

            // check out hash and if already have resolved the Uri
            // don't resolve again.
            namespaceMaps = _namespaceMapHashList[xmlNamespace] as NamespaceMapEntry[];

            if (null == namespaceMaps)
            {
                ArrayList namespaceMapArray = new ArrayList(6);

                // this is the first time the XmlNamespace has been requested
                // add items from the namespaceMap and then walk the assembly
                // list to find matching items.

                if (null != _namespaceMaps)
                {
                    // need to find out the total number of entries by asking each hash
                    // review, may be better to sort these firt time through
                    // assumption here that the namespaceMap entries are small.
                    for (int i = 0; i < _namespaceMaps.Length; i++)
                    {
                        NamespaceMapEntry namespaceMap = _namespaceMaps[i];

                        if (namespaceMap.XmlNamespace == xmlNamespace)
                        {
                            namespaceMapArray.Add(namespaceMap);
                        }
                    }
                }


                List<ClrNamespaceAssemblyPair> namespaceAssemblyPair;
                // Check for Processing instructions for mapping.  If that fails,
                // check for Xmlns definition files.
                if (PITable.Contains(xmlNamespace))
                {
                    namespaceAssemblyPair = new List<ClrNamespaceAssemblyPair>(1);
                    namespaceAssemblyPair.Add((ClrNamespaceAssemblyPair)PITable[xmlNamespace]);
                }
                else
                {
                    namespaceAssemblyPair = GetClrNamespacePairFromCache(xmlNamespace);
                }

                // now walk through any using statements we got and add them
                if (null != namespaceAssemblyPair)
                {

                    for (int j = 0; j < namespaceAssemblyPair.Count; j++)
                    {
                        ClrNamespaceAssemblyPair mapping = namespaceAssemblyPair[j];
                        string          usingAssemblyName = null;
                        string          path =  AssemblyPathFor(mapping.AssemblyName);

                        // using could either have an assembly or not, if it does
                        // have an assembly just need to add this one entry, if
                        // no assembly map in the referenced assemblies.

                        if (
#if PBTCOMPILER
                            mapping.LocalAssembly ||
#endif
                            (!(String.IsNullOrEmpty(mapping.AssemblyName)) && !(String.IsNullOrEmpty(mapping.ClrNamespace))) )
                        {
                            usingAssemblyName = mapping.AssemblyName;
                            NamespaceMapEntry nsMap = new NamespaceMapEntry(xmlNamespace,
                                usingAssemblyName,mapping.ClrNamespace,path);

#if PBTCOMPILER
                            nsMap.LocalAssembly = mapping.LocalAssembly;
                            // if xaml has local components, then it could have internals
                            // but there is no way to determine this until pass2. But we
                            // need to set this here in order to generate the InternalTypeHelper
                            // in pass1.
                            if (nsMap.LocalAssembly)
                            {
                                _hasLocalReference = true;
                            }
#endif

                            namespaceMapArray.Add(nsMap);
                        }

                        if (!String.IsNullOrEmpty(mapping.ClrNamespace))
                        {
                            // Also add in any assembly from the AssemblyNames collection
                            for (int k = 0; k < _assemblyNames.Length; k++)
                            {
                                if (usingAssemblyName == null)
                                {
                                    namespaceMapArray.Add(new NamespaceMapEntry(xmlNamespace,
                                                      _assemblyNames[k],
                                                      mapping.ClrNamespace,
                                                      path));
                                }
                                else
                                {
                                    // If we have a using Assembly, then only add assemblies from
                                    // AssemblyNames that match the using Assembly.
                                    int charIndex = _assemblyNames[k].LastIndexOf('\\');
                                    if (charIndex > 0 &&
                                        _assemblyNames[k].Substring(charIndex + 1) == usingAssemblyName)
                                    {
                                        namespaceMapArray.Add(new NamespaceMapEntry(xmlNamespace,
                                                        _assemblyNames[k],
                                                        mapping.ClrNamespace,
                                                        path));
                                    }
                                }
                            }
                        }
                    }
                }

                // convert to a namespaceMap
                namespaceMaps = (NamespaceMapEntry[]) namespaceMapArray.ToArray(typeof(NamespaceMapEntry));

                // add to hash even if not items so we don't do this work again.
                if (null != namespaceMaps)
                {
                    _namespaceMapHashList.Add(xmlNamespace,namespaceMaps);
                }
            }

            return namespaceMaps;
        }
Beispiel #2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="assemblyNames">Assemblies XamlTypeMapper should use when resolving XAML</param>
        /// <param name="namespaceMaps">NamespaceMap the XamlTypeMapper should use when resolving XAML</param>
        public XamlTypeMapper(
            string[] assemblyNames,
            NamespaceMapEntry[] namespaceMaps)
        {
            if(null == assemblyNames)
            {
                throw new ArgumentNullException( "assemblyNames" );
            }

            _assemblyNames = assemblyNames;
            _namespaceMaps = namespaceMaps;
#if PBTCOMPILER
            _hasInternals = false;
            _hasLocalReference = false;
#endif
        }
Beispiel #3
0
        /// <summary>
        /// Return the Type that corresponds to a localName within a given clr namespace
        /// in a given assembly
        /// </summary>
        /// <param name="namespaceMap">Specifies the CLR namespace and Assembly to look in</param>
        /// <param name="localName">The name of the type</param>
        /// <param name="knownTypesOnly">Search only known types</param>
        /// <returns>Type of object that corresponds to localName</returns>
        private Type GetObjectType(
            NamespaceMapEntry namespaceMap,
            string            localName,
            bool              knownTypesOnly)
        {
            Debug.Assert(namespaceMap.ClrNamespace != null,"Null CLR Namespace");
            Debug.Assert(localName != null,"Null localName");

            Type type = null;

            // Construct the full type name by concatenating the clr namespace and the local name

            if (knownTypesOnly)
            {
                short typeID = BamlMapTable.GetKnownTypeIdFromName(namespaceMap.AssemblyName, namespaceMap.ClrNamespace, localName);
                if (typeID != 0)
                {
                    type = BamlMapTable.GetKnownTypeFromId(typeID);
                }
            }
            else
            {
               Assembly assembly ;
#if PBTCOMPILER
                try
                {
#endif
                    // This may not work if the assembly is not present at compile time, so allow for that.
                    // At runtime it had better be there, so don't ignore it.
                    assembly = namespaceMap.Assembly;
#if PBTCOMPILER
                }
                catch (FileNotFoundException)
                {
                    assembly = null;
                }
#endif

                if (null != assembly)
                {
                    // Type loads may fail if all the prerequisite assemblies haven't been loaded
                    // yet.  In this case, try one more time after loaded all assemblies that the
                    // compiler may have told the XamlTypeMapper about.
                    string fullTypeName = namespaceMap.ClrNamespace + "." + localName;
                    try
                    {
                        type = assembly.GetType(fullTypeName);

                    }
                    catch (Exception e)
                    {
                        if (CriticalExceptions.IsCriticalException(e))
                        {
                           throw;
                        }
                        else
                        {
                            if (LoadReferenceAssemblies())
                            {
                                try
                                {
                                    type = assembly.GetType(fullTypeName);
                                }
                                catch (ArgumentException)
                                {
                                    // A null type is allowable, and the caller will catch it
                                    type = null;
                                }
                            }
                        }
                    }
                }
            }

            return type;
        }
Beispiel #4
0
 private static bool IsInternalAllowedOnType(NamespaceMapEntry namespaceMap)
 {
     bool isInternalAllowed = namespaceMap.LocalAssembly || IsFriendAssembly(namespaceMap.Assembly);
     _hasInternals = _hasInternals || isInternalAllowed;
     return isInternalAllowed;
 }
 internal BamlWriterXamlTypeMapper(
     string[] assemblyNames,
     NamespaceMapEntry[] namespaceMaps) : base(assemblyNames, namespaceMaps)
 {
 }
 public XamlTypeMapper(string[] assemblyNames, NamespaceMapEntry[] namespaceMaps)
 {
 }
Beispiel #7
0
            // Returns a Hashtable of ns -> NamespaceMapEntry[] containing all mappings that are
            // different from what would be returned by the shared context.
            // We don't use _typeMapper.NamespaceMapHashList because we don't want to duplicate all
            // the reflection done by the shared SchemaContext.
            // Thread safety: we assume this is called on the parser thread, and so it's safe to
            // iterate the _typeMapper's data structures.
            internal Hashtable GetNamespaceMapHashList()
            {
                // Copy the ctor-provided namespace mappings into the result
                Hashtable result = new Hashtable();

                if (_typeMapper._namespaceMaps != null)
                {
                    foreach (NamespaceMapEntry mapEntry in _typeMapper._namespaceMaps)
                    {
                        NamespaceMapEntry clone = new NamespaceMapEntry
                        {
                            XmlNamespace = mapEntry.XmlNamespace,
                            ClrNamespace = mapEntry.ClrNamespace,
                            AssemblyName = mapEntry.AssemblyName,
                            AssemblyPath = mapEntry.AssemblyPath
                        };
                        AddToMultiHashtable(result, mapEntry.XmlNamespace, clone);
                    }
                }

                // Copy the Mapping PIs into the result
                foreach (DictionaryEntry piEntry in _typeMapper.PITable)
                {
                    ClrNamespaceAssemblyPair mapping  = (ClrNamespaceAssemblyPair)piEntry.Value;
                    NamespaceMapEntry        mapEntry = new NamespaceMapEntry
                    {
                        XmlNamespace = (string)piEntry.Key,
                        ClrNamespace = mapping.ClrNamespace,
                        AssemblyName = mapping.AssemblyName,
                        AssemblyPath = _typeMapper.AssemblyPathFor(mapping.AssemblyName)
                    };
                    AddToMultiHashtable(result, mapEntry.XmlNamespace, mapEntry);
                }

                // Add any clr-namespaces that were resolved using custom assembly paths
                lock (syncObject)
                {
                    foreach (string clrNs in _clrNamespaces)
                    {
                        string clrNamespace, assembly;
                        SplitClrNsUri(clrNs, out clrNamespace, out assembly);
                        if (!string.IsNullOrEmpty(assembly))
                        {
                            string assemblyPath = _typeMapper.AssemblyPathFor(assembly);
                            if (!string.IsNullOrEmpty(assemblyPath))
                            {
                                NamespaceMapEntry mapEntry = new NamespaceMapEntry
                                {
                                    XmlNamespace = clrNs,
                                    ClrNamespace = clrNamespace,
                                    AssemblyName = assembly,
                                    AssemblyPath = assemblyPath
                                };
                                AddToMultiHashtable(result, mapEntry.XmlNamespace, mapEntry);
                            }
                        }
                    }
                }

                // Convert all the lists to arrays
                object[] keys = new object[result.Count];
                result.Keys.CopyTo(keys, 0);
                foreach (object key in keys)
                {
                    List <NamespaceMapEntry> list = (List <NamespaceMapEntry>)result[key];
                    result[key] = list.ToArray();
                }

                return(result);
            }