2012-01-27 43 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); 
     } 
    } 
} 
相关问题