加载没有NGEN的所有组件

时间:2014-03-17 19:00:10

标签: vb.net

我有一个与Devexpress Control有很多形式的应用程序。问题是当我第一次打开表单时有很长的延迟,这种情况发生在所有表单中。  我不想使用NGEN,而且我使用代码在我的登录表单上预加载所有程序集。  这是代码:

Public Sub DoPrecompile()
    For Each an As System.Reflection.AssemblyName In System.Reflection.Assembly.GetEntryAssembly().GetReferencedAssemblies()
        Dim asmb As System.Reflection.Assembly = System.Reflection.Assembly.Load(an)
        If Not asmb.GlobalAssemblyCache Then
            For Each type As Type In asmb.GetTypes()
                If Not type.IsInterface AndAlso Not type.IsGenericTypeDefinition Then
                    'must initiate only classes without static constractors
                    Dim ci As System.Reflection.ConstructorInfo = type.GetConstructor(System.Reflection.BindingFlags.[Static] Or System.Reflection.BindingFlags.[Public] Or System.Reflection.BindingFlags.NonPublic, Nothing, type.EmptyTypes, Nothing)
                    If ci = Nothing Then
                        For Each method As System.Reflection.MethodInfo In type.GetMethods(System.Reflection.BindingFlags.DeclaredOnly Or System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Public] Or System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.[Static])
                            If Not method.IsAbstract AndAlso Not method.IsGenericMethodDefinition AndAlso Not method.ContainsGenericParameters Then
                                System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(method.MethodHandle)
                            End If
                        Next

                        For Each prop As PropertyInfo In type.GetProperties(BindingFlags.DeclaredOnly Or BindingFlags.NonPublic Or BindingFlags.[Public] Or BindingFlags.Instance Or BindingFlags.[Static])
                            For Each accessor As MethodInfo In prop.GetAccessors(True)
                                System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(accessor.MethodHandle)
                            Next
                        Next
                    End If
                End If
            Next
        End If
    Next
End Sub

我在启动表单上运行此代码,但我收到错误:

System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(accessor.MethodHandle)

未处理的类型' System.ArgumentNullException'发生在mscorlib.dll

附加信息:无法准备抽象方法

我该怎么办?

谢谢!

0 个答案:

没有答案