简体   繁体   English

无法在仅反射的上下文中获取MethodInfo实例

[英]Can't get MethodInfo instance in reflection-only context

The following code shows method's name correctly: 以下代码正确显示了方法的名称:

static void Main(string[] args)
{
    try
    {
        Assembly assembly = Assembly.Load("ClassLibrary1");
        foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
        {
            Assembly.Load(referencedAssembly.Name);
        }
        Type bl1Type = assembly.GetType("ClassLibrary1.Bl1");
        var types = new[] {typeof(MyEnum) };
        var method = bl1Type.GetMethod("Method1", BindingFlags.Instance | BindingFlags.Public, Type.DefaultBinder, types, null);
        Console.WriteLine(method == null ? "Method was null" : $"Found {method.Name}");
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

But if I try to resolve Method1 in a reflection-only context to improve performance and change the call to Assembly.Load("ClassLibrary1"); 但是,如果我尝试在仅反射的上下文中解析Method1以提高性能并更改对Assembly.Load("ClassLibrary1");的调用Assembly.Load("ClassLibrary1"); with Assembly.ReflectionOnlyLoad("ClassLibrary1"); Assembly.ReflectionOnlyLoad("ClassLibrary1"); then method is always null and don't get resolved. 然后method始终为null,并且不会解析。 Any ideas about how to resolve a method in a reflection-only context? 关于如何在仅反射的上下文中解析方法的任何想法?

If you try the following you will see that your method is found in the list 如果您尝试以下操作,您将看到在列表中找到了您的方法

var methods = bl1Type.GetMethods(BindingFlags.Instance | BindingFlags.Public);

Also,if you search for the method that accepts the string parameter it will also work 另外,如果您搜索接受string参数的方法,那么它也将起作用

var types = new[] {typeof(string)};

I think it's confused by the MyEnum. 我认为MyEnum对此感到困惑。
If you load the MyEnum from the second dll it will work 如果从第二个dll加载MyEnum,它将起作用

Assembly assembly2 = Assembly.ReflectionOnlyLoad("ClassLibrary2");
Type bl2Type = assembly2.GetType("ClassLibrary2.MyEnum");
var types = new[] { bl2Type };

The full code 完整代码

using System;
using System.Linq;
using System.Reflection;

namespace ReflectionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.ReflectionOnlyLoad("ClassLibrary1");
            Type bl1Type = assembly.GetType("ClassLibrary1.Bl1");
            var types = new[] { Assembly.ReflectionOnlyLoad(assembly.GetReferencedAssemblies().Single(a => a.Name == "ClassLibrary2").Name).GetType("ClassLibrary2.MyEnum") };
            //var types = new[] {typeof(MyEnum)}; //doesn't work
            var method = bl1Type.GetMethod("Method1", BindingFlags.Instance | BindingFlags.Public, Type.DefaultBinder, types, null);
            Console.WriteLine(method == null ? "Method was null" : $"Found {method.Name}");
            Console.ReadLine();
        }
    }
}

So,your initial code doesn't work because it tries to find a method that uses a different "MyEnum" (and of course it doesn't exist) 因此,您的初始代码不起作用,因为它试图找到使用其他“ MyEnum”的方法(当然它不存在)

ReflectionOnly context is separate loader context from the regular loader context. ReflectionOnly上下文是独立于常规加载程序上下文的加载程序上下文。 It loads own copy of the assemblies and types, the system types from mscorlib are shared with the regular loader context though. 它加载程序集和类型的自己的副本,但是mscorlib中的系统类型与常规加载程序上下文共享。

Also, there is typically no performance advantage in loading assemblies into ReflectionOnly context. 另外,将程序集加载到ReflectionOnly上下文中通常没有性能优势。 ReflectionOnly context uses the exact same loading mechanism as regular assemblies, except that attempt to execute any code from them is blocked and a few sanity checks are suppressed ( https://blogs.msdn.microsoft.com/junfeng/2004/08/24/reflection-only-assembly-loading/ describes the details). ReflectionOnly上下文使用与常规程序集完全相同的加载机制,不同之处在于,阻止从它们执行任何代码的尝试并抑制了一些健全性检查( https://blogs.msdn.microsoft.com/junfeng/2004/08/24 / reflection-only-assembly-loading /描述详细信息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不应加载引用程序集以供执行。 它们只能在 Reflection-only loader 上下文中加载 - Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context .Net中的执行上下文和仅反射上下文是什么? - What are execution context and reflection-only context in .Net? 反射:如果类型参数是TypeBuilder,则无法在类BindingList &lt;&gt;中获取“添加”的MethodInfo - Reflection: can't get a MethodInfo for 'Add' in class BindingList<> if the type argument is a TypeBuilder 从Action <T>的实例获取Invoke MethodInfo的最安全方法 - Safest way to get the Invoke MethodInfo from Action<T>'s Instance 如何使用Reflection将构造函数作为MethodInfo获取 - How to get constructor as MethodInfo using Reflection 获取ICollection的MethodInfo <T> - Get MethodInfo for ICollection<T> 通过反射获取String.Trim的MethodInfo? - Get MethodInfo of String.Trim with reflection? 如何使用反射从MethodInfo获取WebGetAttribute - How to Get WebGetAttribute from MethodInfo Using Reflection 获取具有反射的String.TrimStart的MethodInfo? - Get MethodInfo of String.TrimStart with reflection? 使用Reflection.Emit在堆栈上使用MethodInfo实例调用方法 - Call a method using a MethodInfo instance on the stack using Reflection.Emit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM