ReflectionOnlyLoad()
最終調用專用nLoad()
方法true
爲forIntrospection
參數。另一方面,LoadWithPartialName()
(其具有您希望的程序集查找行爲)還使用不同的參數集(和forIntrospection
)委託給nLoad()
。用反思複製部分調用是一個簡單的反思問題。 :)
更新:其實並不那麼簡單。如果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);
}
如果你看一下在Visual Studio中的參考屬性,你會看到組件的版本號 – 2009-10-01 22:17:57
系統應該已經在非反射唯一的國家被加載。 – 2009-10-01 22:32:43