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

标签 c# assemblies appdomain

好的,这是交易:

我想将用户定义的 Assembly 加载到我的 AppDomain 中,但是我只想在指定的 Assembly 符合一些要求。在我的例子中,除其他要求外,它必须有一个 Assembly 级别的 Attribute 我们可以调用 MandatoryAssemblyAttribute

据我所知,有两条路可以走:

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

  2. 我可以创建一个新的 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 的情况下遍历加载的程序集?我做错了什么?

非常感谢您的任何建议。

最佳答案

The problem I see with this solution is that you actually have to know a type (full name) in the Assembly

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

这里有一些代码可以让你开始。这些类进入您自己的程序集(不是您要检查的程序集):

第一个类用于创建检查 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);
         }
      }
   }
}

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

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;
   }
}

关于c# - 如果存在某个属性,则加载程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7919545/

相关文章:

c# - 在浏览器中打开 URL 并更改应用程序 View - Android Xamarin

.net - 我应该什么时候将程序集部署到 GAC 中?

.net - LoadFrom 和上下文

c# - 无法为 x64 和 x86 加载文件或程序集 'CefSharp.Wpf;只有一个作品

c# - 创建 Func 以返回引用类型和值类型

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

c# - 如何从未引用的程序集中获取类型?

Java AppDomain 之类的抽象?

c# - 在不从父域引用程序集的情况下从子 AppDomain 检索数据

c# - 如何正确P/Invoke这个函数?