我有一个这样的课程:
Public Class ClassPermissions
Public Class Configuration
Public Const EditPermissions As String = "[Edit Permissions]"
Public Const OpenConfigPanel As String = "[Open Configuration Panel]"
End Class
Public Class Access
Public Const AccessCarForm As String = "[Access Car Form]"
Public Const AccessCarList As String = "[Access Car List]"
End Class
''' <summary>
''' Verifies permission for the logged on user
''' </summary>
''' <param name="permissao">Permission Required, ClassPermissions.(...)</param>
''' <returns>True if user has permission</returns>
''' <remarks></remarks>
Public Shared Function HasPermission(permission As String) As Boolean
Dim ret As Boolean = False
Dim sqlQuery As String = ""
Dim dt As DataTable
sqlQuery = "SELECT P." & permission & " FROM Permissions AS P INNER JOIN Users AS C ON P.idUser = C.UserID AND C.UserID = " & vggLoggedUserID & " AND C.Active = 1"
dt = GetGenericDataTable(sqlQuery, "Permission")
If dt.Rows.Count > 0 AndAlso dt.Rows(0)(0) = "True" Then
ret = True
End If
Return ret
End Function
End Class
我怎么能说'HasPermission'函数的参数'permission'只能是在Class ClassPermissions里面的类中声明的常量?
我想要的是,当某人编码并写入'ClassPermissions.HasPermission('然后它会显示一个常量列表,这种情况是'EditPermissions','OpenConfigPanel','AccessCarForm'和'AccessCarList'。
答案 0 :(得分:1)
将数据库中的实际列名称作为字符串常量公开可能是个坏主意。
声明一个枚举:
Public Enum Configuration
EditPermissions
OpenConfigPanel
End Enum
将您的方法更改为
Public Shared Function HasPermission(permission As Configuration) As Boolean
在方法内部,确保在枚举中定义permission
并将其映射到相应的字符串值。例如。拥有可能值为Private Dictionary(Of Configuration, String)
并使用TryGetValue()
。