[英]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.