本文整理汇总了C#中System.ComponentModel.Composition.Hosting.AssemblyCatalog.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# AssemblyCatalog.Dispose方法的具体用法?C# AssemblyCatalog.Dispose怎么用?C# AssemblyCatalog.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ComponentModel.Composition.Hosting.AssemblyCatalog
的用法示例。
在下文中一共展示了AssemblyCatalog.Dispose方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddAssemblyCatalogs
/// <summary>
/// This adds assembly catalogs to the given aggregate catalog for the given folder and all of its
/// subfolders recursively.
/// </summary>
/// <param name="catalog">The aggregate catalog to which the assembly catalogs are added.</param>
/// <param name="folder">The root folder to search. It and all subfolders recursively will be searched
/// for assemblies to add to the aggregate catalog.</param>
/// <param name="searchedFolders">A hash set of folders that have already been searched and added.</param>
/// <param name="includeSubfolders">True to search subfolders recursively, false to only search the given
/// folder.</param>
/// <remarks>It is done this way to prevent a single assembly that would normally be discovered via a
/// directory catalog from preventing all assemblies from loading if it cannot be examined when the parts
/// are composed (i.e. trying to load a Windows Store assembly on Windows 7).</remarks>
private static void AddAssemblyCatalogs(AggregateCatalog catalog, string folder,
HashSet<string> searchedFolders, bool includeSubfolders)
{
if(!String.IsNullOrWhiteSpace(folder) && Directory.Exists(folder) && !searchedFolders.Contains(folder))
{
foreach(var file in Directory.EnumerateFiles(folder, "*.dll"))
{
try
{
var asmCat = new AssemblyCatalog(file);
// Force MEF to load the assembly and figure out if there are any exports. Valid
// assemblies won't throw any exceptions and will contain parts and will be added to
// the catalog. Use Count() rather than Any() to ensure it touches all parts in case
// that makes a difference.
if(asmCat.Parts.Count() > 0)
catalog.Catalogs.Add(asmCat);
else
asmCat.Dispose();
}
catch(FileNotFoundException ex)
{
// Ignore the errors we may expect to see but log them for debugging purposes
System.Diagnostics.Debug.WriteLine(ex);
}
catch(FileLoadException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
catch(BadImageFormatException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
catch(IOException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
catch(System.Security.SecurityException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
catch(UnauthorizedAccessException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
catch(TypeLoadException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
catch(ReflectionTypeLoadException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
foreach(var lex in ex.LoaderExceptions)
System.Diagnostics.Debug.WriteLine(lex);
}
}
// Enumerate subfolders separately so that we can skip future requests for the same folder
if(includeSubfolders)
try
{
foreach(string subfolder in Directory.EnumerateDirectories(folder, "*", SearchOption.AllDirectories))
AddAssemblyCatalogs(catalog, subfolder, searchedFolders, false);
}
catch(IOException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
catch(System.Security.SecurityException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
catch(UnauthorizedAccessException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
示例2: RegisterExtensions
private static void RegisterExtensions(AggregateCatalog catalog, IEnumerable<string> enumerateFiles, IConsole console)
{
foreach (var item in enumerateFiles)
{
AssemblyCatalog assemblyCatalog = null;
try
{
assemblyCatalog = new AssemblyCatalog(item);
// get the parts - throw if something went wrong
var parts = assemblyCatalog.Parts;
// load all the types - throw if assembly cannot load (missing dependencies is a good example)
var assembly = Assembly.LoadFile(item);
assembly.GetTypes();
catalog.Catalogs.Add(assemblyCatalog);
}
catch (BadImageFormatException ex)
{
if (assemblyCatalog != null)
{
assemblyCatalog.Dispose();
}
// Ignore if the dll wasn't a valid assembly
console.WriteWarning(ex.Message);
}
catch (FileLoadException ex)
{
// Ignore if we couldn't load the assembly.
if (assemblyCatalog != null)
{
assemblyCatalog.Dispose();
}
var message =
String.Format(LocalizedResourceManager.GetString(nameof(NuGetResources.FailedToLoadExtension)),
item);
console.WriteWarning(message);
console.WriteWarning(ex.Message);
}
catch (ReflectionTypeLoadException rex)
{
// ignore if the assembly is missing dependencies
var resource =
LocalizedResourceManager.GetString(nameof(NuGetResources.FailedToLoadExtensionDuringMefComposition));
var perAssemblyError = string.Empty;
if (rex?.LoaderExceptions.Length > 0)
{
var builder = new StringBuilder();
builder.AppendLine(string.Empty);
var errors = rex.LoaderExceptions.Select(e => e.Message).Distinct(StringComparer.Ordinal);
foreach (var error in errors)
{
builder.AppendLine(error);
}
perAssemblyError = builder.ToString();
}
var warning = string.Format(resource, item, perAssemblyError);
console.WriteWarning(warning);
}
}
}