【问题标题】:NGen for partial trust application用于部分信任应用程序的 NGen
【发布时间】:2012-01-27 17:42:04
【问题描述】:

我有一个 .Net 4.0 应用程序,我需要提高在部分信任环境中运行的代码的性能。具体来说,我想消除运行时 JIT 的需要。通常这是使用 NGEN (http://http://msdn.microsoft.com/en-us/library/6t9t5wcf(v=vs.100).aspx) 完成的,但这不适用于以部分信任运行的程序集。我还有其他选择吗?

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

【问题讨论】:

    标签: .net-4.0 jit ngen partial-trust


    【解决方案1】:

    我最终做的是通过PrepareMethod 方法在运行时执行JIT。我没有在不受信任的应用程序内部执行此操作,而是在应用程序的完全受信任部分执行此操作,然后将类型发送到部分受信任的沙箱以运行。我使用了类似于陈立然博客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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 2010-09-27
      相关资源
      最近更新 更多