2011-01-23 40 views
1

我發現這個代碼:如何翻譯這個C#代碼來檢測當前信任級別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代碼,但它產生我VW​​D內部的錯誤:

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 

很明顯,需要有人通過處理NT在C#和VB.NET,可以在VB.NET

發現錯誤

感謝

拉爾斯

+1

嘗試加載它在反射器,並選擇顯示它在VB.Net。通常工作。 http://reflector.red-gate.com/download.aspx?TreatAsUpdate=1 –

+0

感謝您的提示!下一次我遇到類似的C#vs VB.NET問題時,我會研究一下。 Lars – lars

回答

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方法。
+0

但是你是一個VB.Net專家:-) 你解決了它! 非常好!謝謝! Lars – lars

+0

@lars很高興能爲您服務:) –