如何转换此C#代码以检测ASP.NET应用程序的当前信任级别到VB.Net

时间:2011-01-23 18:24:49

标签: asp.net vb.net

我找到了这段代码:

AspNetHostingPermissionLevel GetCurrentTrustLevel() {
    foreach (AspNetHostingPermissionLevel trustLevel in
            new AspNetHostingPermissionLevel [] {
                AspNetHostingPermissionLevel.Unrestricted,
                AspNetHostingPermissionLevel.High,
                AspNetHostingPermissionLevel.Medium,
                AspNetHostingPermissionLevel.Low,
                AspNetHostingPermissionLevel.Minimal 
            } )
    {
        try {
            new AspNetHostingPermission(trustLevel).Demand();
        }
        catch (System.Security.SecurityException ) {
            continue;
        }

        return trustLevel;
    }

    return AspNetHostingPermissionLevel.None;
}

来自Get current ASP.NET Trust Level programmatically,但这是C#,我想将它用于VB.NET。 任何有VB.NET和C#专家的人都有机会将其翻译成VB代码吗?

我尝试了自己并获得了以下VB.NET代码,但它在我的VWD中生成错误:

Private Function GetCurrentTrustLevel() As AspNetHostingPermissionLevel

    For Each trustLevel As AspNetHostingPermissionLevel In New _
        AspNetHostingPermissionLevel() { _
            AspNetHostingPermissionLevel.Unrestricted, _
            AspNetHostingPermissionLevel.High, _
            AspNetHostingPermissionLevel.Medium, _
            AspNetHostingPermissionLevel.Low, _
            AspNetHostingPermissionLevel.Minimal _
        }

            Try
                New AspNetHostingPermission(trustLevel).Demand()
            Catch generatedExceptionName As System.Security.SecurityException
                Continue Try
            End Try

            Return trustLevel
        Next

        Return AspNetHostingPermissionLevel.None
    End Function

这些部分似乎错了:

New AspNetHostingPermission(trustLevel).Demand()

Continue Try

显然,需要由能够熟练使用C#和VB.NET的人来处理,并且能够发现VB.NET中的错误

由于

拉​​斯

1 个答案:

答案 0 :(得分:1)

以下内容应该有效

Private Function GetCurrentTrustLevel() As AspNetHostingPermissionLevel

    For Each trustLevel As AspNetHostingPermissionLevel In New _
        AspNetHostingPermissionLevel() { _
            AspNetHostingPermissionLevel.Unrestricted, _
            AspNetHostingPermissionLevel.High, _
            AspNetHostingPermissionLevel.Medium, _
            AspNetHostingPermissionLevel.Low, _
            AspNetHostingPermissionLevel.Minimal _
        }

            Try
                Dim HostedPermission As AspNetHostingPermission = _
                    New AspNetHostingPermission(trustLevel)
                HostedPermission.Demand()
            Catch generatedExceptionName As System.Security.SecurityException
                Continue For
            End Try

            Return trustLevel
        Next

        Return AspNetHostingPermissionLevel.None
    End Function

我不是VB.Net专家,但以下是我为使其工作而采取的措施:

  1. Continue For而不是Continue Try
  2. 需要声明AspNetHostingPermission类型的变量并对其进行初始化,然后调用Demand方法。