Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 从程序集获取信息而不加载它_.net_Reflection_.net Assembly - Fatal编程技术网

.net 从程序集获取信息而不加载它

.net 从程序集获取信息而不加载它,.net,reflection,.net-assembly,.net,Reflection,.net Assembly,我的应用程序有一个插件结构,它将DLL(bog标准.NET程序集)作为插件加载。我有一个应用程序范围的选项,可以直接从磁盘加载这些dll(Assembly.LoadFrom(file)),也可以先将dll复制到内存中,然后从字节数组加载(Assembly.load(IO.file.ReadAllBytes(file))) 我想为插件开发人员添加一个选项,让他们选择是否强制执行特定的加载行为。我想我应该使用AssemblyAttributes,然后ReflectionOnly加载DLL以查看是否定

我的应用程序有一个插件结构,它将DLL(bog标准.NET程序集)作为插件加载。我有一个应用程序范围的选项,可以直接从磁盘加载这些dll(
Assembly.LoadFrom(file)
),也可以先将dll复制到内存中,然后从字节数组加载(
Assembly.load(IO.file.ReadAllBytes(file))

我想为插件开发人员添加一个选项,让他们选择是否强制执行特定的加载行为。我想我应该使用AssemblyAttributes,然后ReflectionOnly加载DLL以查看是否定义了该属性。但是,我无法使用GetCustomAttributesData获取此信息,因为DLL依赖于未仅加载reflectiononly的其他程序集。我现在发现自己在玩卡夫卡式的打鼹鼠游戏

插件开发人员在加载DLL之前与我的应用程序通信的好方法是什么?AssemblyAttributes是一种方法吗?如果是,如何确保reflectiononly加载永远不会失败

编辑:

我引用Mono.Cecil来迭代程序集属性。我第一次使用塞西尔,希望我做得对。在我的开发人员机器上的初始测试似乎有效

Private Function ExtractAssemblyLoadBehaviour(ByVal file As String) As GH_LoadingBehaviour
  Try
    If (Not IO.File.Exists(file)) Then Return GH_LoadingBehaviour.ApplicationDefault

    Dim assembly As AssemblyDefinition = AssemblyDefinition.ReadAssembly(file)
    If (assembly Is Nothing) Then Return GH_LoadingBehaviour.ApplicationDefault

    Dim attributes As Collection(Of CustomAttribute) = assembly.CustomAttributes
    If (attributes Is Nothing) Then Return GH_LoadingBehaviour.ApplicationDefault
    For Each Attribute As CustomAttribute In attributes
      Dim type As TypeReference = Attribute.AttributeType
      If (type.FullName.Contains("GH_CoffLoadingAttribute")) Then Return GH_LoadingBehaviour.ForceCOFF
      If (type.FullName.Contains("GH_DirectLoadingAttribute")) Then Return GH_LoadingBehaviour.ForceDirect
    Next

    Return GH_LoadingBehaviour.ApplicationDefault
  Catch ex As Exception
    Return GH_LoadingBehaviour.ApplicationDefault
  End Try
End Function

反射只加载还是加载,所以一旦你这样做了,问这个问题就太晚了

一种选择是在单独的AppDomain中执行仅反射加载,然后将结果返回主代码,并丢弃新的AppDomain


使用属性的另一种选择是要求插件开发人员包含某种清单文件(例如文本或XML),其中包含您需要的任何信息或选项。

为什么同时提供这两种选项?只是好奇…在这一点上,这主要是遗产。我希望能够从远程位置加载DLL,由于某些原因,某些DLL需要从磁盘加载。我希望将此作为一个选项删除,并在每个dll的基础上决定什么是最好的,但我希望我的开发人员对发生的事情保持一定的控制。