MissingMethodException thrown by GetExportedTypes

2019-07-07 10:44发布

I'm getting a MissingMethodException thrown when I call GetExportedTypes, the code:

Assembly.LoadFrom(assemblyPath).GetExportedTypes();

The exception (names obfuscated):

System.MissingMethodException was unhandled
  Message="Method not found: 'Void Namespace.IMyMethod.MyMethod(UInt32, Namespace.IMyOtherMethod ByRef, UInt32 ByRef)'."
  Source="mscorlib"
  StackTrace:
       at System.Reflection.Assembly._GetExportedTypes()
       at System.Reflection.Assembly.GetExportedTypes()
       at ConsoleApplication1.Program.Main(String[] args) in C:\Documents and Settings\jpealing\My Documents\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 16
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

The exception has no inner exception or other details worth mentioning.

What causes this? How can I fix it?

Reflector has no problems loading this assembly:

  • It is registered in the GAC
  • It only references mscorlib.
  • I didn't build the assembly - there is only 1 version of this assembly on my machine.

Update:

The assembly in question appears to be an assembly built using Tlbexp.exe - it seems likely that this is making some sort of difference.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-07-07 11:21

As far as I can see it is simply expected that certain assemblies throw this exception - the GetTypes method throws a similar exception - ReflectionTypeLoadException, however this exception also has a Types property which appears to be the list of types that were successfully loaded.

For now I have adapted my code so that it calls this method instead and catches the resulting exception:

try
(
    return assembly.GetTypes();
)
catch (ReflectionTypeLoadException ex)
{
    return ex.Types;
}

Its not pretty, however it does appear to work.

查看更多
霸刀☆藐视天下
3楼-- · 2019-07-07 11:32

I have seen something similar when I was converting a project of mine to .NET 4.

While my issue occurred in PEVERIFY, it hinted to the same problem.

If using .NET 4 on a older assembly, change the security policy back to the older one.

Here's the MSDN link.

查看更多
登录 后发表回答