2012-01-27 42 views
0

我有一個.net 4.0應用程序,我需要提高在部分信任環境中運行的代碼的性能。具體來說,我想在運行時消除對JIT的需求。通常這是通過使用NGEN(http://http://msdn.microsoft.com/en-us/library/6t9t5wcf(v=vs.100).aspx)完成的,但這對於部分信任運行的程序集不起作用。我有其他選擇嗎?NGen爲部分信任應用程序

Native images that are generated with Ngen.exe can no longer be loaded into 
applications that are running in partial trust. 

回答

0

我最終做的是在運行時通過PrepareMethod方法執行JIT。我不是在不受信任的應用程序內執行此操作,而是在將類型發送到部分受信任的沙箱中運行之前,在應用程序的完全信任部分執行此操作。我使用了一種類似於Liran Chen博客上發現的機制here

public static void PreJITMethods(Assembly assembly) 
{ 
    Type[] types = assembly.GetTypes(); 
    foreach (Type curType in types) 
    { 
     MethodInfo[] methods = curType.GetMethods(
      BindingFlags.DeclaredOnly | 
      BindingFlags.NonPublic | 
      BindingFlags.Public | 
      BindingFlags.Instance | 
      BindingFlags.Static); 

     foreach (MethodInfo curMethod in methods) 
     { 
      if (curMethod.IsAbstract || curMethod.ContainsGenericParameters) 
       continue; 

      RuntimeHelpers.PrepareMethod(curMethod.MethodHandle); 
     } 
    } 
} 
相關問題