是否可以在编译时将具体值与字段相关联?

时间:2015-05-07 20:14:53

标签: .net vb.net

我不确定如何准确地说出来。我有一个名为BoundedString的类,基本上看起来像:

Public Class BoundedString

    Public Property Value() As String
    Public Property MinLength() As Integer
    Public Property MaxLength() As Integer

End Class

在我的代码的其他地方,我希望能够说:

Public Class SomeClass
    Public Property StringField() As BoundedString(Of 3, 5)
End Class

其中3和5表示MinLength和MaxLength的值。

这样,我可以静态指定有关我的字符串的信息。我发现的最接近的东西似乎是泛型或属性,但我不认为泛型让你指定具体的价值观。是否有人知道这样做的语言功能?

2 个答案:

答案 0 :(得分:3)

您可能需要重新定义New()方法:

Public Class BoundedString

    Public Property Value As String
    Public Property MinLength As Integer
    Public Property MaxLength As Integer

    Public Sub New(myMinLength As Integer, myMaxLength As Integer)
        MinLength = myMinLength
        MaxLength = myMaxLength
    End Sub

End Class

Public Class SomeClass

    Dim _StringField As BoundedString
    Public Property StringField As BoundedString
        Get
            Return _StringField
        End Get
        Set(value As BoundedString)
            If value.MinLength = 3 AndAlso value.MaxLength = 5 Then
                _StringField = value
            End If
        End Set
    End Property

End Class

答案 1 :(得分:0)

以下是我如何使用属性完成此操作。代码的工作原理是:

  1. 定义一组属性,MinLengthAttributeMaxLengthAttribute,可用于字段或属性。
  2. 定义一个方法GetPropertyAttribute,该方法从对象的属性中检索属性。
  3. 在构建GetPropertyAttribute时使用BoundedString检索GetPropertyAttribute的最小和最大长度。
  4. 这允许您将长度直接关联到属性旁边,这使得类定义相当容易阅读。不可否认,必须使用<AttributeUsage(AttributeTargets.Field Or AttributeTargets.Property)> Public Class MinLengthAttribute : Inherits Attribute Public Property MinLength As Integer ''' <param name="minimumLength">The minimum length of this field/property.</param> Public Sub New(ByVal minimumLength As Integer) minimumLength = minimumLength End Sub End Class <AttributeUsage(AttributeTargets.Field Or AttributeTargets.Property)> Public Class MaxLengthAttribute : Inherits Attribute Public Property MaxLength As Integer ''' <param name="maximumLength">The maximum length of this field/property.</param> Public Sub New(ByVal maximumLength As Integer) MaxLength = maximumLength End Sub End Class ''' <summary> ''' Gets the attribute TAttribute of the property <paramref name="strPropertyName" /> on this class. ''' </summary> ''' <typeparam name="TAttribute">The type of the attribute to retrieve.</typeparam> ''' <param name="oObj">An object that has a property named <paramref name="strPropertyName"/>.</param> ''' <param name="strPropertyName">The property to get an attribute from.</param> ''' <remarks> ''' Requires that <paramref name="strPropertyName"/> has only one attribute ''' of type TAttribute. Throws an exception if this is not the case. ''' </remarks> <Extension> Public Function GetPropertyAttribute(Of TAttribute As Attribute)(oObj As Object, ByVal strPropertyName As String) As TAttribute Dim piPropertyInfo As PropertyInfo = oObj.GetType().GetProperty(strPropertyName) Dim aobjAttributes As Object() = piPropertyInfo.GetCustomAttributes(True) Dim lstAttributes As List(Of TAttribute) = (From objAttribute As Attribute In aobjAttributes.OfType(Of TAttribute)() Select CType(objAttribute, TAttribute)).ToList() If lstAttributes.Count = 1 Then Return lstAttributes(0) Else Throw New InvalidOperationException("Property " & strPropertyName & " has " & lstAttributes.Count & " attributes of provided type. GetPropertyAttribute requires that" & strPropertyName & " only has one property of the provided type.") End If End Function Public Class SomeClass <MinLength(0)> <MaxLength(30)> Public Property String1 As BoundedString <MinLength(5)> <MaxLength(10)> Public Property String2 As BoundedString <MinLength(0)> <MaxLength(100)> Public Property String3 As BoundedString Public Sub New(ByVal string1 As String, ByVal string2 As String, ByVal string3 As String) String1 = New BoundedString(string1, GetPropertyAttribute(Of MinLengthAttribute)("String1").MinLength, GetPropertyAttribute(Of MaxLengthAttribute)("String1").MaxLength) String2 = New BoundedString(string1, GetPropertyAttribute(Of MinLengthAttribute)("String2").MinLength, GetPropertyAttribute(Of MaxLengthAttribute)("String2").MaxLength) String3 = New BoundedString(string1, GetPropertyAttribute(Of MinLengthAttribute)("String3").MinLength, GetPropertyAttribute(Of MaxLengthAttribute)("String3").MaxLength) End Sub End Class 来检索最小/最大长度值有点不稳定。

    function openGallery(url)
    {    
      $.colorbox({width:"80%", height:"80%", iframe:true, href:"/pagetoopen.html"});
    }