Ejemplo n.º 1
0
            /// <summary>
            /// Determine if a given type name is in the assembly or not. Return null if the type is not in the assembly
            /// </summary>
            internal LoadedType GetLoadedTypeByTypeName(string typeName)
            {
                ErrorUtilities.VerifyThrowArgumentNull(typeName, "typeName");

                // Only one thread should be doing operations on this instance of the object at a time.

                Type type = _typeNameToType.GetOrAdd(typeName, (key) =>
                {
                    if ((_assemblyLoadInfo.AssemblyName != null) && (typeName.Length > 0))
                    {
                        try
                        {
                            // try to load the type using its assembly qualified name
                            Type t2 = Type.GetType(typeName + "," + _assemblyLoadInfo.AssemblyName, false /* don't throw on error */, true /* case-insensitive */);
                            if (t2 != null)
                            {
                                return(!_isDesiredType(t2, null) ? null : t2);
                            }
                        }
                        catch (ArgumentException)
                        {
                            // Type.GetType() will throw this exception if the type name is invalid -- but we have no idea if it's the
                            // type or the assembly name that's the problem -- so just ignore the exception, because we're going to
                            // check the existence/validity of the assembly and type respectively, below anyway
                        }
                    }

                    if (Interlocked.Read(ref _haveScannedPublicTypes) == 0)
                    {
                        lock (_lockObject)
                        {
                            if (Interlocked.Read(ref _haveScannedPublicTypes) == 0)
                            {
                                ScanAssemblyForPublicTypes();
                                Interlocked.Exchange(ref _haveScannedPublicTypes, ~0);
                            }
                        }
                    }

                    foreach (KeyValuePair <string, Type> desiredTypeInAssembly in _publicTypeNameToType)
                    {
                        // if type matches partially on its name
                        if (typeName.Length == 0 || TypeLoader.IsPartialTypeNameMatch(desiredTypeInAssembly.Key, typeName))
                        {
                            return(desiredTypeInAssembly.Value);
                        }
                    }

                    return(null);
                });

                return(type != null ? new LoadedType(type, _assemblyLoadInfo, _loadedAssembly) : null);
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Determine if a given type name is in the assembly or not. Return null if the type is not in the assembly
            /// </summary>
            internal LoadedType GetLoadedTypeByTypeName(string typeName)
            {
                ErrorUtilities.VerifyThrowArgumentNull(typeName, "typeName");

                // Only one thread should be doing operations on this instance of the object at a time.
                lock (_lockObject)
                {
                    Type type = null;

                    // Maybe we've already cracked open this assembly before. Check to see if the typeName is in the list we don't look for partial matches here
                    // this is an optimization.
                    bool foundType = _typeNameToType.TryGetValue(typeName, out type);
                    if (!foundType)
                    {
                        // We could still not find the type, lets try and resolve it by doing a get type.
                        if ((_assemblyLoadInfo.AssemblyName != null) && (typeName.Length > 0))
                        {
                            try
                            {
                                // try to load the type using its assembly qualified name
                                type = Type.GetType(typeName + "," + _assemblyLoadInfo.AssemblyName, false /* don't throw on error */, true /* case-insensitive */);
                            }
                            catch (ArgumentException)
                            {
                                // Type.GetType() will throw this exception if the type name is invalid -- but we have no idea if it's the
                                // type or the assembly name that's the problem -- so just ignore the exception, because we're going to
                                // check the existence/validity of the assembly and type respectively, below anyway
                            }

                            // if we found the type, it means its assembly qualified name was also its fully qualified name
                            if (type != null)
                            {
                                // if it's not the right type, bail out -- there's no point searching further since we already matched on the
                                // fully qualified name
                                if (!_isDesiredType(type, null))
                                {
                                    _typeNameToType.Add(typeName, null);
                                    return(null);
                                }
                                else
                                {
                                    _typeNameToType.Add(typeName, type);
                                }
                            }
                        }

                        // We could not find the type based on the passed in type name, we now need to see if there is a type which
                        // will match based on partially matching the typename. To do this partial matching we need to get the public types in the assembly
                        if (type == null && !_haveScannedPublicTypes)
                        {
                            ScanAssemblyForPublicTypes();
                            _haveScannedPublicTypes = true;
                        }

                        // Could not find the type we need to look through the types in the assembly or in our cache.
                        if (type == null)
                        {
                            foreach (KeyValuePair <string, Type> desiredTypeInAssembly in _publicTypeNameToType)
                            {
                                // if type matches partially on its name
                                if (typeName.Length == 0 || TypeLoader.IsPartialTypeNameMatch(desiredTypeInAssembly.Key, typeName))
                                {
                                    type = desiredTypeInAssembly.Value;
                                    _typeNameToType.Add(typeName, type);
                                    break;
                                }
                            }
                        }
                    }

                    if (type != null)
                    {
                        return(new LoadedType(type, _assemblyLoadInfo, _loadedAssembly));
                    }

                    return(null);
                }
            }