通过反射调用方法会导致 System.MissingMethodException
我正在动态加载程序集并从中调用静态方法。当该方法使用不重要的引用(例如 mscorlib 或 System.Core)时,就会出现问题 - 我收到 System.MissingMethodException 。我尝试过检查已加载程序集的引用程序集并手动加载它们,从而强制将它们加载到 AppDomain 上。我已检查 CurrentDomain.GetAssemblies
,程序集已加载。
该参考是第 3 方库,我知道已加载正确的版本(来自 CurrentDomain.GetAssemblies
)。
什么可能导致这个问题?
I am loading an assembly dynamically and invoking a static method from it. The problem arises when the method uses a reference which is not trivial (e.g. mscorlib or System.Core) - I get System.MissingMethodException
. I have tried going through the references assemblies of the loaded assembly and manually loading them all, thus forcing them to be loaded onto the AppDomain. I have checked CurrentDomain.GetAssemblies
, the assemblies are loaded.
The reference is a 3rd party library and I know the correct version is loaded (from CurrentDomain.GetAssemblies
).
What could cause this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一定不是第一个在 8 年后发现这个问题的人...
当使用
AssemblyResolve
或ReflectionOnlyAssemblyResolve
从自定义位置解析程序集时,您必须确保程序集通过加载的实例(例如Assembly.Load
)是唯一的,因为相同的名称可能会传递到AssemblyResolve
中... AppDomain 本身很混乱,因为它没有缓存加载的程序集!微软加油,你真的可以更好地记录这一点!
I musn't be the first one figured this out after 8 years...
When using
AssemblyResolve
orReflectionOnlyAssemblyResolve
to resolve assemblies from custom location, you have to make sure the assembly instance loaded through, sayAssembly.Load
, is unique, as same names might be passed intoAssemblyResolve
... The AppDomain is confusing itself, as it is not caching the loaded assemblies!!Come on microsoft, you really can document this better!
您使用什么方法来调用该静态方法?
我会使用:
如果万一该方法实际上在基类中实现,您将需要:
如果该方法不是公共的,则必须添加
BindingFlags.NonPublic
。当您希望从外部程序集中获取它时,请使用
Assembly.GetType()
而不是typeof
。What method are you using for invoking that static method?
I would use:
If by any chance the method is actually implemented in the base class you will need:
If the method is not public, you will have to add
BindingFlags.NonPublic
.As you want it from the external assembly, use
Assembly.GetType()
instead oftypeof
.