开发者

How to translate this C# code to detect the current Trust Level for ASP.NET Application into VB.Net

I found this code:

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;
}

from Get current ASP.NET Trust Level programmatically, but this is C#, and I would like to have it for VB.NET. Any chance of someone expert in both VB.NET and C# that can translate this to VB code?

I tried myself and got the following VB.NET code but it generates an error inside my VWD:

Private Function GetCurrentTrustLevel() As AspNetHostingPermissionLevel

    For Each trustLevel As AspNetHostingPermissionLevel In New _
        AspNetHostingPermissionLevel() { _
            AspNetHostingPermissionLevel.Unrestricted, _
            AspNetHostingPermissionLevel.High, _
            AspNetHostingPermissionLev开发者_如何学运维el.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

These parts seems to be wrong:

New AspNetHostingPermission(trustLevel).Demand()

and

Continue Try

Obviously, need to be handled by someone fluent in both C# and VB.NET and can spot the errors in VB.NET

Thanks

Lars


The following should work

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

I am no VB.Net expert, but the following is what I did to get it to work:

  1. Continue For rather than Continue Try
  2. Needed to declare a variable of type AspNetHostingPermission and initialize it, then call the Demand method.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜