通过反射调用方法会导致 System.MissingMethodException

发布于 2024-12-19 18:24:15 字数 311 浏览 0 评论 0原文

我正在动态加载程序集并从中调用静态方法。当该方法使用不重要的引用(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心在旅行 2024-12-26 18:24:15

我一定不是第一个在 8 年后发现这个问题的人...

当使用 AssemblyResolveReflectionOnlyAssemblyResolve 从自定义位置解析程序集时,您必须确保程序集通过加载的实例(例如Assembly.Load)是唯一的,因为相同的名称可能会传递到AssemblyResolve中... AppDomain 本身很混乱,因为它没有缓存加载的程序集!

    private static Dictionary<string, Assembly> _resolved = new Dictionary<string, Assembly>();

    private static Assembly ResolveDependencies(object sender, ResolveEventArgs args)
    {
        if (_resolved.ContainsKey(args.Name))
        {
            return _resolved[args.Name];
        }

        var asm = Assembly.Load(GetEmbeddedAssembly(args.Name));
        _resolved[args.Name] = asm;
        return asm;
    }

微软加油,你真的可以更好地记录这一点!

I musn't be the first one figured this out after 8 years...

When using AssemblyResolve or ReflectionOnlyAssemblyResolve to resolve assemblies from custom location, you have to make sure the assembly instance loaded through, say Assembly.Load, is unique, as same names might be passed into AssemblyResolve... The AppDomain is confusing itself, as it is not caching the loaded assemblies!!

    private static Dictionary<string, Assembly> _resolved = new Dictionary<string, Assembly>();

    private static Assembly ResolveDependencies(object sender, ResolveEventArgs args)
    {
        if (_resolved.ContainsKey(args.Name))
        {
            return _resolved[args.Name];
        }

        var asm = Assembly.Load(GetEmbeddedAssembly(args.Name));
        _resolved[args.Name] = asm;
        return asm;
    }

Come on microsoft, you really can document this better!

所谓喜欢 2024-12-26 18:24:15

您使用什么方法来调用该静态方法?

我会使用:

typeof(YourType).GetMethod("YourMethodName", 
     BindingFlags.Public | BindingFlags.Static).Invoke(null, your_params);

如果万一该方法实际上在基类中实现,您将需要:

typeof(YourType).GetMethod("YourMethodName", 
     BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
  .Invoke(null, your_params); 

如果该方法不是公共的,则必须添加 BindingFlags.NonPublic

当您希望从外部程序集中获取它时,请使用 Assembly.GetType() 而不是 typeof

What method are you using for invoking that static method?

I would use:

typeof(YourType).GetMethod("YourMethodName", 
     BindingFlags.Public | BindingFlags.Static).Invoke(null, your_params);

If by any chance the method is actually implemented in the base class you will need:

typeof(YourType).GetMethod("YourMethodName", 
     BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
  .Invoke(null, your_params); 

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 of typeof.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文