如果存在某个属性,则加载程序集

Posted

技术标签:

【中文标题】如果存在某个属性,则加载程序集【英文标题】:Loading an Assembly if a certain Attribute is present 【发布时间】:2011-10-27 16:57:57 【问题描述】:

好的,这是交易:

我想将用户定义的Assembly 加载到我的AppDomain 中,但是我只想在指定的Assembly 符合某些要求时才这样做。就我而言,除了其他要求外,它还必须具有Assembly 级别Attribute,我们可以称之为MandatoryAssemblyAttribute

就我所见,我可以走两条路:

    Assembly 加载到我当前的AppDomain 并检查Attribute 是否存在。简单但不方便,因为即使它没有MandatoryAssemblyAttribute,我也会被加载的程序集困住。不好。

    我可以创建一个新的AppDomain 并从那里加载Assembly 并检查我的旧MandatoryAddemblyAttribute 是否存在。如果是,则转储创建的 AppDomain 并继续将Assembly 加载到我的CurrentAppDomain 中,如果不是,则转储新的AppDomain,告诉用户,让他再试一次。

    李>

说起来容易做起来难。在网上搜索,我找到了几个关于如何解决这个问题的例子,包括这个之前发布的问题 SO:Loading DLLs into a separate AppDomain

我看到这个解决方案的问题是,您实际上必须知道您想要加载的Assembly 中的类型(全名)。这不是我喜欢的解决方案。重点是尝试插入符合某些要求的任意Assembly,并通过属性发现要使用的类型。事先不知道Assembly 将具有什么类型。当然,我可以要求任何打算以这种方式使用的Assembly 都应该实现一些虚拟类,以便为CreateInstanceFromAndUnwrap 提供“入口点”。我宁愿不要。

另外,如果我继续沿着这条线做点什么:

using (var frm = new OpenFileDialog())

    frm.DefaultExt = "dll";
    frm.Title = "Select dll...";
    frm.Filter = "Model files (*.dll)|*.dll";
    answer = frm.ShowDialog(this);

     if (answer == DialogResult.OK)
     
          domain = AppDomain.CreateDomain("Model", new Evidence(AppDomain.CurrentDomain.Evidence));

          try
          
              domain.CreateInstanceFrom(frm.FileName, "DummyNamespace.DummyObject");
                    modelIsValid = true;
          
          catch (TypeLoadException)
          
              ...
          
          finally
          
              if (domain != null)
                   AppDomain.Unload(domain);
          
     

这会很好,但如果这样我继续执行以下操作:

foreach (var ass in domain.GetAssemblies()) //Do not fret, I would run this before unloading the AppDomain
    Console.WriteLine(ass.FullName); 

我收到了FileNotFoundException。为什么?

我可以采取的另一条路是这条路:How load DLL in separate AppDomain 但我也没有运气。每当我选择一些随机的 .NET Assembly 时,我都会得到一个 FileNotFoundException,此外它违背了目的,因为我需要知道程序集的名称(而不是文件名)才能加载它,这不符合我的要求.

除了MEF(我的目标不是.NET 3.5)之外,还有其他方法吗?还是我坚持创建一些虚拟对象以通过CreateInstanceFromAndUnwrap 加载Assembly?如果是这样,为什么我不能在没有得到FileNotFoundException 的情况下遍历加载的程序集?我做错了什么?

非常感谢您的建议。

【问题讨论】:

【参考方案1】:

我看到这个解决方案的问题是你实际上必须知道 程序集中的类型(全名)

这不太准确。您需要知道的是类型名称是某个程序集,不一定是您要检查的程序集。您应该在您的程序集中创建一个 MarshalByRef 类,然后使用 CreateInstanceAndUnwrap 从您自己的程序集(不是您要检查的程序集)创建它的实例。然后该类将执行加载(因为它位于新的 appdomain 中)并检查并将布尔结果返回给原始 appdomain。

这里有一些代码可以帮助你。这些类放在您自己的程序集中(不是您要检查的程序集):

第一个类用于创建考试 AppDomain 并创建 MarshalByRefObject 类的实例(见底部):

using System;
using System.Security.Policy;

internal static class AttributeValidationUtility

   internal static bool ValidateAssembly(string pathToAssembly)
   
      AppDomain appDomain = null;
      try
      
         appDomain = AppDomain.CreateDomain("ExaminationAppDomain", new Evidence(AppDomain.CurrentDomain.Evidence));

         AttributeValidationMbro attributeValidationMbro = appDomain.CreateInstanceAndUnwrap(
                              typeof(AttributeValidationMbro).Assembly.FullName,
                              typeof(AttributeValidationMbro).FullName) as AttributeValidationMbro;

         return attributeValidationMbro.ValidateAssembly(pathToAssembly);
      
      finally
      
         if (appDomain != null)
         
            AppDomain.Unload(appDomain);
         
      
   

这是实际存在于新 AppDomain 中的 MarshalByRefObject,将对程序集进行实际检查:

using System;
using System.Reflection;

public class AttributeValidationMbro : MarshalByRefObject

   public override object InitializeLifetimeService()
   
      // infinite lifetime
      return null;
   

   public bool ValidateAssembly(string pathToAssembly)
   
      Assembly assemblyToExamine = Assembly.LoadFrom(pathToAssembly);

      bool hasAttribute = false;

      // TODO: examine the assemblyToExamine to see if it has the attribute

      return hasAttribute;
   

【讨论】:

谢谢!那真的很有帮助。我从来没有想过要这样做,但现在我已经看到了,这似乎很明显......呃!【参考方案2】:

这可以通过使用托管程序集阅读器轻松完成,例如Mono.Cecil。您将检查程序集是否用属性修饰,而不在 AppDomain 中加载程序集,实际上,根本不会弄乱 AppDomains。以塞西尔为例:

bool HasMandatoryAttribute (string fileName)

    return AssemblyDefinition.ReadAssembly (fileName)
        .CustomAttributes
        .Any (attribute => attribute.AttributType.Name == "MandatoryAttribute");

这基本上是他大部分插件系统所做的事情,因为创建一个 AppDomain 并将其拆除对于运行时来说是相当昂贵的。

【讨论】:

您应该为此获得更多支持。这非常易于使用,并且避免了多次加载程序集或必须编组的问题。

以上是关于如果存在某个属性,则加载程序集的主要内容,如果未能解决你的问题,请参考以下文章

warning MSB3245: 未能解析此引用。未能找到程序集“CemeteryBLL”。请检查磁盘上是否存在该程序集。 如果您的代码需要此引用,则可能出现编译错误。

如何检查某个程序集是不是存在?

由于版本冲突,无法加载某个程序集

asp.net提示“未能加载文件或程序集“XXXXXXXX.dll”或它的某一个依赖项。找不到指定的模块。”

混合模式程序集是针对版本X构建的,如果没有其他配置信息,则无法在运行时的版本Y中加载

未能加载文件或程序集怎么解决