示例#1
0
        /// <summary>
        /// Load an assembly given its AssemblyLoadInfo
        /// </summary>
        /// <param name="assemblyLoadInfo"></param>
        /// <returns></returns>
        private static Assembly LoadAssembly(AssemblyLoadInfo assemblyLoadInfo)
        {
            Assembly loadedAssembly = null;

            try
            {
                if (assemblyLoadInfo.AssemblyName != null)
                {
#if !FEATURE_ASSEMBLYLOADCONTEXT
                    loadedAssembly = Assembly.Load(assemblyLoadInfo.AssemblyName);
#else
                    loadedAssembly = Assembly.Load(new AssemblyName(assemblyLoadInfo.AssemblyName));
#endif
                }
                else
                {
#if !FEATURE_ASSEMBLYLOADCONTEXT
                    loadedAssembly = Assembly.UnsafeLoadFrom(assemblyLoadInfo.AssemblyFile);
#else
                    loadedAssembly = s_coreClrAssemblyLoader.LoadFromPath(assemblyLoadInfo.AssemblyFile);
#endif
                }
            }
            catch (ArgumentException e)
            {
                // Assembly.Load() and Assembly.LoadFrom() will throw an ArgumentException if the assembly name is invalid
                // convert to a FileNotFoundException because it's more meaningful
                // NOTE: don't use ErrorUtilities.VerifyThrowFileExists() here because that will hit the disk again
                throw new FileNotFoundException(null, assemblyLoadInfo.AssemblyLocation, e);
            }

            return(loadedAssembly);
        }
示例#2
0
 /// <summary>
 /// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
 /// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
 /// found will be returned.
 /// </summary>
 /// <returns>The loaded type, or null if the type was not found.</returns>
 internal LoadedType ReflectionOnlyLoad
 (
     string typeName,
     AssemblyLoadInfo assembly
 )
 {
     return(GetLoadedType(s_cacheOfReflectionOnlyLoadedTypesByFilter, typeName, assembly));
 }
示例#3
0
 /// <summary>
 /// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
 /// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
 /// found will be returned.
 /// </summary>
 internal LoadedType Load
 (
     string typeName,
     AssemblyLoadInfo assembly
 )
 {
     return(GetLoadedType(s_cacheOfLoadedTypesByFilterLock, s_loadInfoToTypeLock, s_cacheOfLoadedTypesByFilter, typeName, assembly));
 }
示例#4
0
            /// <summary>
            /// Given a type filter, and an assembly to load the type information from determine if a given type name is in the assembly or not.
            /// </summary>
            internal AssemblyInfoToLoadedTypes(Func <Type, object, bool> typeFilter, AssemblyLoadInfo loadInfo)
            {
                ErrorUtilities.VerifyThrowArgumentNull(typeFilter, "typefilter");
                ErrorUtilities.VerifyThrowArgumentNull(loadInfo, "loadInfo");

                _isDesiredType        = typeFilter;
                _assemblyLoadInfo     = loadInfo;
                _typeNameToType       = new ConcurrentDictionary <string, Type>(StringComparer.OrdinalIgnoreCase);
                _publicTypeNameToType = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);
            }
示例#5
0
            /// <summary>
            /// Given a type filter, and an assembly to load the type information from determine if a given type name is in the assembly or not.
            /// </summary>
            internal AssemblyInfoToLoadedTypes(TypeFilter typeFilter, AssemblyLoadInfo loadInfo)
            {
                ErrorUtilities.VerifyThrowArgumentNull(typeFilter, "typefilter");
                ErrorUtilities.VerifyThrowArgumentNull(loadInfo, "loadInfo");

                this.isDesiredType        = typeFilter;
                this.assemblyLoadInfo     = loadInfo;
                this.typeNameToType       = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);
                this.publicTypeNameToType = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);
            }
示例#6
0
        /// <summary>
        /// Creates an instance of this class for the given type.
        /// </summary>
        /// <param name="type">The Type to be loaded</param>
        /// <param name="assemblyLoadInfo">Information used to load the assembly</param>
        /// <param name="loadedAssembly">The assembly which has been loaded, if any</param>
        internal LoadedType(Type type, AssemblyLoadInfo assemblyLoadInfo, Assembly loadedAssembly)
        {
            ErrorUtilities.VerifyThrow(type != null, "We must have the type.");
            ErrorUtilities.VerifyThrow(assemblyLoadInfo != null, "We must have the assembly the type was loaded from.");

            _type = type;
            _assembly = assemblyLoadInfo;
            _loadedAssembly = loadedAssembly;

            CheckForHardcodedSTARequirement();
            HasLoadInSeparateAppDomainAttribute();
            HasSTAThreadAttribute();
        }
示例#7
0
        /// <summary>
        /// Creates an instance of this class for the given type.
        /// </summary>
        /// <param name="type">The Type to be loaded</param>
        /// <param name="assemblyLoadInfo">Information used to load the assembly</param>
        /// <param name="loadedAssembly">The assembly which has been loaded, if any</param>
        internal LoadedType(Type type, AssemblyLoadInfo assemblyLoadInfo, Assembly loadedAssembly)
        {
            ErrorUtilities.VerifyThrow(type != null, "We must have the type.");
            ErrorUtilities.VerifyThrow(assemblyLoadInfo != null, "We must have the assembly the type was loaded from.");

            _type           = type;
            _assembly       = assemblyLoadInfo;
            _loadedAssembly = loadedAssembly;

            CheckForHardcodedSTARequirement();
            HasLoadInSeparateAppDomainAttribute();
            HasSTAThreadAttribute();
        }
示例#8
0
        /// <summary>
        /// Load an assembly given its AssemblyLoadInfo
        /// </summary>
        /// <param name="assemblyLoadInfo"></param>
        /// <returns></returns>
        private static Assembly LoadAssembly(AssemblyLoadInfo assemblyLoadInfo)
        {
            Assembly loadedAssembly = null;

            try
            {
                if (assemblyLoadInfo.AssemblyName != null)
                {
#if FEATURE_ASSEMBLY_LOADFROM
                    loadedAssembly = Assembly.Load(assemblyLoadInfo.AssemblyName);
#else
                    loadedAssembly = Assembly.Load(new AssemblyName(assemblyLoadInfo.AssemblyName));
#endif
                }
                else
                {
#if FEATURE_ASSEMBLY_LOADFROM
                    loadedAssembly = Assembly.UnsafeLoadFrom(assemblyLoadInfo.AssemblyFile);
#else
                    // If the Assembly is provided via a file path, the following rules are used to load the assembly:
                    // - if the simple name of the assembly exists in the same folder as msbuild.exe, then that assembly gets loaded, indifferent of the user specified path
                    // - otherwise, the assembly from the user specified path is loaded, if it exists.

                    var assemblyNameInExecutableDirectory = Path.Combine(BuildEnvironmentHelper.Instance.CurrentMSBuildToolsDirectory,
                                                                         Path.GetFileName(assemblyLoadInfo.AssemblyFile));

                    if (FileSystems.Default.FileExists(assemblyNameInExecutableDirectory))
                    {
                        var simpleName = Path.GetFileNameWithoutExtension(assemblyLoadInfo.AssemblyFile);
                        loadedAssembly = Assembly.Load(new AssemblyName(simpleName));
                    }
                    else
                    {
                        var baseDir = Path.GetDirectoryName(assemblyLoadInfo.AssemblyFile);
                        s_coreClrAssemblyLoader.AddDependencyLocation(baseDir);
                        loadedAssembly = s_coreClrAssemblyLoader.LoadFromPath(assemblyLoadInfo.AssemblyFile);
                    }
#endif
                }
            }
            catch (ArgumentException e)
            {
                // Assembly.Load() and Assembly.LoadFrom() will throw an ArgumentException if the assembly name is invalid
                // convert to a FileNotFoundException because it's more meaningful
                // NOTE: don't use ErrorUtilities.VerifyThrowFileExists() here because that will hit the disk again
                throw new FileNotFoundException(null, assemblyLoadInfo.AssemblyLocation, e);
            }

            return(loadedAssembly);
        }
示例#9
0
        /// <summary>
        /// Determines if two AssemblyLoadInfos are effectively the same.
        /// </summary>
        public override bool Equals(Object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            AssemblyLoadInfo otherAssemblyInfo = obj as AssemblyLoadInfo;

            if (otherAssemblyInfo == null)
            {
                return(false);
            }

            return((this.AssemblyName == otherAssemblyInfo.AssemblyName) && (this.AssemblyFile == otherAssemblyInfo.AssemblyFile));
        }
示例#10
0
        internal void CreateFromStream(BinaryReader reader)
        {
            #region LoggerClassName
            if (reader.ReadByte() == 0)
            {
                _loggerClassName = null;
            }
            else
            {
                _loggerClassName = reader.ReadString();
            }
            #endregion 
            #region LoggerSwitchParameters
            if (reader.ReadByte() == 0)
            {
                _loggerSwitchParameters = null;
            }
            else
            {
                _loggerSwitchParameters = reader.ReadString();
            }
            #endregion
            #region LoggerAssembly
            if (reader.ReadByte() == 0)
            {
                _loggerAssembly = null;
            }
            else
            {
                string assemblyName = null;
                string assemblyFile = null;

                if (reader.ReadByte() != 0)
                {
                    assemblyFile = reader.ReadString();
                }

                if (reader.ReadByte() != 0)
                {
                    assemblyName = reader.ReadString();
                }

                _loggerAssembly = AssemblyLoadInfo.Create(assemblyName, assemblyFile);
            }
            #endregion
            _verbosity = (LoggerVerbosity)reader.ReadInt32();
            _loggerId = reader.ReadInt32();
        }
示例#11
0
 /// <summary>
 /// Creates an instance of this class for the given type.
 /// </summary>
 internal LoadedType(Type type, AssemblyLoadInfo assemblyLoadInfo)
     : this(type, assemblyLoadInfo, null)
 {
 }
示例#12
0
            /// <summary>
            /// Given a type filter, and an assembly to load the type information from determine if a given type name is in the assembly or not.
            /// </summary>
            internal AssemblyInfoToLoadedTypes(TypeFilter typeFilter, AssemblyLoadInfo loadInfo)
            {
                ErrorUtilities.VerifyThrowArgumentNull(typeFilter, "typefilter");
                ErrorUtilities.VerifyThrowArgumentNull(loadInfo, "loadInfo");

                _isDesiredType = typeFilter;
                _assemblyLoadInfo = loadInfo;
                _typeNameToType = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
                _publicTypeNameToType = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
            }
示例#13
0
        /// <summary>
        /// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
        /// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
        /// found will be returned.
        /// </summary>
        private LoadedType GetLoadedType(object cacheLock, object loadInfoToTypeLock, ConcurrentDictionary<TypeFilter, ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes>> cache, string typeName, AssemblyLoadInfo assembly)
        {
            // A given typefilter have been used on a number of assemblies, Based on the typefilter we will get another dictionary which 
            // will map a specific AssemblyLoadInfo to a AssemblyInfoToLoadedTypes class which knows how to find a typeName in a given assembly.
            ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes> loadInfoToType = null;
            lock (cacheLock)
            {
                if (!cache.TryGetValue(_isDesiredType, out loadInfoToType))
                {
                    loadInfoToType = new ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes>();
                    cache.TryAdd(_isDesiredType, loadInfoToType);
                }
            }

            // Get an object which is able to take a typename and determine if it is in the assembly pointed to by the AssemblyInfo.
            AssemblyInfoToLoadedTypes typeNameToType = null;
            lock (loadInfoToTypeLock)
            {
                if (!loadInfoToType.TryGetValue(assembly, out typeNameToType))
                {
                    typeNameToType = new AssemblyInfoToLoadedTypes(_isDesiredType, assembly);
                    loadInfoToType.TryAdd(assembly, typeNameToType);
                }
            }

            return typeNameToType.GetLoadedTypeByTypeName(typeName);
        }
示例#14
0
 /// <summary>
 /// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
 /// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
 /// found will be returned.
 /// </summary>
 /// <returns>The loaded type, or null if the type was not found.</returns>
 internal LoadedType ReflectionOnlyLoad
 (
     string typeName,
     AssemblyLoadInfo assembly
 )
 {
     return GetLoadedType(s_cacheOfReflectionOnlyLoadedTypesByFilterLock, s_reflectionOnlyloadInfoToTypeLock, s_cacheOfReflectionOnlyLoadedTypesByFilter, typeName, assembly);
 }
示例#15
0
        /// <summary>
        /// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
        /// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
        /// found will be returned.
        /// </summary>
        private LoadedType GetLoadedType(ConcurrentDictionary <Func <Type, object, bool>, ConcurrentDictionary <AssemblyLoadInfo, AssemblyInfoToLoadedTypes> > cache, string typeName, AssemblyLoadInfo assembly)
        {
            // A given type filter have been used on a number of assemblies, Based on the type filter we will get another dictionary which
            // will map a specific AssemblyLoadInfo to a AssemblyInfoToLoadedTypes class which knows how to find a typeName in a given assembly.
            ConcurrentDictionary <AssemblyLoadInfo, AssemblyInfoToLoadedTypes> loadInfoToType =
                cache.GetOrAdd(_isDesiredType, (_) => new ConcurrentDictionary <AssemblyLoadInfo, AssemblyInfoToLoadedTypes>());

            // Get an object which is able to take a typename and determine if it is in the assembly pointed to by the AssemblyInfo.
            AssemblyInfoToLoadedTypes typeNameToType =
                loadInfoToType.GetOrAdd(assembly, (_) => new AssemblyInfoToLoadedTypes(_isDesiredType, _));

            return(typeNameToType.GetLoadedTypeByTypeName(typeName));
        }
示例#16
0
 /// <summary>
 /// Creates an instance of this class for the given type.
 /// </summary>
 /// <param name="type"></param>
 /// <param name="assembly"></param>
 internal LoadedType(Type type, AssemblyLoadInfo assemblyLoadInfo)
     : this(type, assemblyLoadInfo, null)
 {
 }
示例#17
0
        /// <summary>
        /// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
        /// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
        /// found will be returned.
        /// </summary>
        private LoadedType GetLoadedType(object cacheLock, object loadInfoToTypeLock, ConcurrentDictionary <TypeFilter, ConcurrentDictionary <AssemblyLoadInfo, AssemblyInfoToLoadedTypes> > cache, string typeName, AssemblyLoadInfo assembly)
        {
            // A given typefilter have been used on a number of assemblies, Based on the typefilter we will get another dictionary which
            // will map a specific AssemblyLoadInfo to a AssemblyInfoToLoadedTypes class which knows how to find a typeName in a given assembly.
            ConcurrentDictionary <AssemblyLoadInfo, AssemblyInfoToLoadedTypes> loadInfoToType = null;

            lock (cacheLock)
            {
                if (!cache.TryGetValue(_isDesiredType, out loadInfoToType))
                {
                    loadInfoToType = new ConcurrentDictionary <AssemblyLoadInfo, AssemblyInfoToLoadedTypes>();
                    cache.TryAdd(_isDesiredType, loadInfoToType);
                }
            }

            // Get an object which is able to take a typename and determine if it is in the assembly pointed to by the AssemblyInfo.
            AssemblyInfoToLoadedTypes typeNameToType = null;

            lock (loadInfoToTypeLock)
            {
                if (!loadInfoToType.TryGetValue(assembly, out typeNameToType))
                {
                    typeNameToType = new AssemblyInfoToLoadedTypes(_isDesiredType, assembly);
                    loadInfoToType.TryAdd(assembly, typeNameToType);
                }
            }

            return(typeNameToType.GetLoadedTypeByTypeName(typeName));
        }
示例#18
0
 /// <summary>
 /// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
 /// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
 /// found will be returned.
 /// </summary>
 internal LoadedType Load
 (
     string typeName,
     AssemblyLoadInfo assembly
 )
 {
     return GetLoadedType(cacheOfLoadedTypesByFilterLock, loadInfoToTypeLock, cacheOfLoadedTypesByFilter, typeName, assembly);
 }
 /// <summary>
 /// Abstract out the creation of the new AssemblyTaskFactory with default task, and 
 /// with some basic validation.
 /// </summary>
 private void SetupTaskFactory(IDictionary<string, string> factoryParameters, bool explicitlyLaunchTaskHost)
 {
     _taskFactory = new AssemblyTaskFactory();
     _loadInfo = AssemblyLoadInfo.Create(null, Assembly.GetAssembly(typeof(TaskToTestFactories)).Location);
     _loadedType = _taskFactory.InitializeFactory(_loadInfo, "TaskToTestFactories", new Dictionary<string, TaskPropertyInfo>(), string.Empty, factoryParameters, explicitlyLaunchTaskHost, null, ElementLocation.Create("NONE"), String.Empty);
     Assert.True(_loadedType.Assembly.Equals(_loadInfo)); // "Expected the AssemblyLoadInfo to be equal"
 }
示例#20
0
        /// <summary>
        /// Initialize the factory from the task registry
        /// </summary>
        internal LoadedType InitializeFactory
            (
                AssemblyLoadInfo loadInfo,
                string taskName,
                IDictionary<string, TaskPropertyInfo> taskParameters,
                string taskElementContents,
                IDictionary<string, string> taskFactoryIdentityParameters,
                bool taskHostFactoryExplicitlyRequested,
                TargetLoggingContext targetLoggingContext,
                ElementLocation elementLocation,
                string taskProjectFile
            )
        {
            ErrorUtilities.VerifyThrowArgumentNull(loadInfo, "loadInfo");
            VerifyThrowIdentityParametersValid(taskFactoryIdentityParameters, elementLocation, taskName, "Runtime", "Architecture");

            if (taskFactoryIdentityParameters != null)
            {
                _factoryIdentityParameters = new Dictionary<string, string>(taskFactoryIdentityParameters, StringComparer.OrdinalIgnoreCase);
            }

            _taskHostFactoryExplicitlyRequested = taskHostFactoryExplicitlyRequested;

            try
            {
                ErrorUtilities.VerifyThrowArgumentLength(taskName, "taskName");
                _taskName = taskName;
                _loadedType = _typeLoader.Load(taskName, loadInfo);
                ProjectErrorUtilities.VerifyThrowInvalidProject(_loadedType != null, elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, String.Empty);
            }
            catch (TargetInvocationException e)
            {
                // Exception thrown by the called code itself
                // Log the stack, so the task vendor can fix their code
                ProjectErrorUtilities.VerifyThrowInvalidProject(false, elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, Environment.NewLine + e.InnerException.ToString());
            }
            catch (ReflectionTypeLoadException e)
            {
                // ReflectionTypeLoadException.LoaderExceptions may contain nulls
                foreach (Exception exception in e.LoaderExceptions)
                {
                    if (exception != null)
                    {
                        targetLoggingContext.LogError(new BuildEventFileInfo(taskProjectFile), "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, exception.Message);
                    }
                }

                ProjectErrorUtilities.VerifyThrowInvalidProject(false, elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, e.Message);
            }
            catch (ArgumentNullException e)
            {
                // taskName may be null
                ProjectErrorUtilities.VerifyThrowInvalidProject(false, elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, e.Message);
            }
            catch (Exception e) // Catching Exception, but rethrowing unless it's a well-known exception.
            {
                if (ExceptionHandling.NotExpectedReflectionException(e))
                {
                    throw;
                }

                ProjectErrorUtilities.VerifyThrowInvalidProject(false, elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, e.Message);
            }

            return _loadedType;
        }
 public void TearDownAttribute()
 {
     _taskFactory = null;
     _loadInfo = null;
     _loadedType = null;
 }