|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// |
| 4 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 7 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 9 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 10 | +// THE SOFTWARE. |
| 11 | +// |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.ComponentModel.Composition; |
| 15 | +using System.ComponentModel.Composition.Hosting; |
| 16 | +using System.IO; |
| 17 | +using System.Linq; |
| 18 | +using System.Reflection; |
| 19 | + |
| 20 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Provides a simple DirectoryCatalog implementation that doesn't |
| 24 | + /// fail assembly loading when it encounters a ReflectionTypeLoadException. |
| 25 | + /// The default DirectoryCatalog implementation stops evaluating any |
| 26 | + /// remaining assemblies in the directory if one of these exceptions |
| 27 | + /// are encountered. |
| 28 | + /// </summary> |
| 29 | + internal class SafeDirectoryCatalog : AggregateCatalog |
| 30 | + { |
| 31 | + public SafeDirectoryCatalog(string folderLocation, IOutputWriter outputWriter) |
| 32 | + { |
| 33 | + if (outputWriter == null) |
| 34 | + { |
| 35 | + throw new ArgumentNullException("outputWriter"); |
| 36 | + } |
| 37 | + |
| 38 | + // Make sure the directory actually exists |
| 39 | + var directoryInfo = new DirectoryInfo(folderLocation); |
| 40 | + if (directoryInfo.Exists == false) |
| 41 | + { |
| 42 | + throw new CompositionException( |
| 43 | + "The specified folder does not exist: " + directoryInfo.FullName); |
| 44 | + } |
| 45 | + |
| 46 | + // Load each DLL found in the directory |
| 47 | + foreach (var dllFile in directoryInfo.GetFileSystemInfos("*.dll")) |
| 48 | + { |
| 49 | + try |
| 50 | + { |
| 51 | + // Attempt to create an AssemblyCatalog for this DLL |
| 52 | + var assemblyCatalog = |
| 53 | + new AssemblyCatalog( |
| 54 | + Assembly.LoadFile( |
| 55 | + dllFile.FullName)); |
| 56 | + |
| 57 | + // We must call ToArray here to pre-initialize the Parts |
| 58 | + // IEnumerable and cause it to be stored. The result is |
| 59 | + // not used here because it will be accessed later once |
| 60 | + // the composition container starts assembling parts. |
| 61 | + assemblyCatalog.Parts.ToArray(); |
| 62 | + |
| 63 | + this.Catalogs.Add(assemblyCatalog); |
| 64 | + } |
| 65 | + catch (ReflectionTypeLoadException e) |
| 66 | + { |
| 67 | + // Write out the exception details and allow the |
| 68 | + // loading process to continue |
| 69 | + outputWriter.WriteWarning(e.ToString()); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments