ReflectionOnlyLoad() 最终以true 为forIntrospection 参数调用私有nLoad() 方法。另一方面,LoadWithPartialName() 具有您想要的程序集查找行为,也使用一组不同的参数委托给nLoad()(false 代表forIntrospection)。用内省复制部分调用是一个简单的反思问题。 :)
更新:其实没那么简单。如果nLoad() 失败,我们需要调用私有EnumerateCache(),然后调用不同的InternalLoad()。以下适用于我的机器:
[Test]
public void TestReflectionOnlyLoadWithPartialName()
{
var l = ReflectionOnlyLoadWithPartialName("System.Windows.Forms");
Assert.IsTrue(l.ReflectionOnly);
}
public Assembly ReflectionOnlyLoadWithPartialName(string partialName)
{
return ReflectionOnlyLoadWithPartialName(partialName, null);
}
public Assembly ReflectionOnlyLoadWithPartialName(string partialName, Evidence securityEvidence)
{
if (securityEvidence != null)
new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Demand();
AssemblyName fileName = new AssemblyName(partialName);
var assembly = nLoad(fileName, null, securityEvidence, null, null, false, true);
if (assembly != null)
return assembly;
var assemblyRef = EnumerateCache(fileName);
if (assemblyRef != null)
return InternalLoad(assemblyRef, securityEvidence, null, true);
return assembly;
}
private Assembly nLoad(params object[] args)
{
return (Assembly)typeof(Assembly)
.GetMethod("nLoad", BindingFlags.NonPublic | BindingFlags.Static)
.Invoke(null, args);
}
private AssemblyName EnumerateCache(params object[] args)
{
return (AssemblyName)typeof(Assembly)
.GetMethod("EnumerateCache", BindingFlags.NonPublic | BindingFlags.Static)
.Invoke(null, args);
}
private Assembly InternalLoad(params object[] args)
{
// Easiest to query because the StackCrawlMark type is internal
return (Assembly)
typeof(Assembly).GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
.First(m => m.Name == "InternalLoad" && m.GetParameters()[0].ParameterType == typeof(AssemblyName))
.Invoke(null, args);
}