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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10091221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:24:29  来源:igfitidea点击:

Loading plug-in DLL files, "The invoked member is not supported in a dynamic assembly."

c#.net-4.0.net-2.0.net-assemblydynamic-assemblies

提问by Trevorm

We have custom DLL's that are not included in our initial setup file. They are loaded at runtime. This process worked fine while using .NET 2.0, but we are getting the "The invoked member is not supported in a dynamic assembly" error message now that we are using .NET 4.0.

我们的初始安装文件中没有包含自定义 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) {}

回答by Trevorm

This in the app.config file allows for "plug-in" dll's from remote sources.

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

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

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

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

回答by user326608

For me this issue was not embedding the license for a Aspose dll: http://www.aspose.com/community/forums/thread/423874/initializing-the-license-file.aspx

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

Their code injects dynamic assemblies when a license isn't detected, causing their DLLs to fail, as well as a bunch of other code that isn't compatible with dynamic assemblies.

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

Not sure if this is a common licensing/activation method for ensuring registered use with 3rd party dlls, so I'll post it here for google if it is.

不确定这是否是确保与 3rd 方 dll 一起注册使用的常见许可/激活方法,因此如果是,我将在此处发布给 google。

回答by Gusdor

This error is occurring because Assembly.Loadcannot be called upon dynamic assemblies. You must filter out the dynamic assemblies before using them.

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

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

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