加载插件DLL文件,“动态程序集中不支持调用的成员”。

时间:2012-04-10 15:02:40

标签: c# .net-4.0 .net-2.0 .net-assembly dynamic-assemblies

我们的初始设置文件中没有自定义DLL。它们在运行时加载。这个过程在使用.NET 2.0时运行良好,但是现在我们正在使用.NET 4.0,我们得到“动态程序集中不支持被调用的成员”错误消息。

try
{
    assem = Assembly.LoadFrom(fi.FullName); //fi is FileSystemInfo
}
catch (FileLoadException) {}
catch (BadImageFormatException) {}
catch (System.Security.SecurityException) {}
catch (ArgumentException) {}
catch (PathTooLongException) {}

5 个答案:

答案 0 :(得分:16)

对我来说,这个问题并没有嵌入Aspose dll的许可证:http://www.aspose.com/community/forums/thread/423874/initializing-the-license-file.aspx

当未检测到许可证时,他们的代码会注入动态程序集,导致其DLL失败,以及一堆与动态程序集不兼容的其他代码。

不确定这是否是确保在第三方dll上注册使用的常用许可/激活方法,因此如果是,我会在此处发布。

答案 1 :(得分:13)

发生此错误是因为无法在动态程序集上调用Assembly.Load。在使用动态装配之前,必须先过滤掉它们。

var assemblies AppDomain.CurrentDomain.GetAssemblies().Where(p => !p.IsDynamic);

答案 2 :(得分:3)

这在app.config文件中允许来自远程源的“插件”dll。

<configuration>
   <runtime>
      <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>

http://msdn.microsoft.com/en-us/library/dd409252.aspx

答案 3 :(得分:0)

我遇到了同样的错误。我们的代码库中有一个方法可以遍历当前 AppDomain 中加载的程序集,并按名称查找给定资源。

        Assembly[] allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
        
        foreach (Assembly tempAssembly in allAssemblies)
        {
            Stream resourceStream = tempAssembly.GetManifestResourceStream(resourceName);
            // ...
            
        }
        

如果我们偶然发现了动态程序集,则对 GetManifestResourceStream 的调用将失败,并显示“动态程序集中不支持调用的成员”错误。

答案 4 :(得分:0)

我花了很多时间来解决这个问题。

我们正在加载另一个 DLL 项目的 Class Library,该项目反过来创建了动态实例。所以下面对我有用。

解决方案:

DLL 的引用添加到主项目。

相关问题