【发布时间】:2014-11-26 16:58:20
【问题描述】:
我想创建一个名为 RangeAttribute 的元数据属性,而不需要像 this answer 中所见的 PostSharp 这样的外部工具,因为它需要库的付费版本。
我发现的唯一官方信息是this MSDN documentation,但荒谬的是,该页面只解释了如何声明类和继承......没有更多,所以我不仅仅是迷路了。
我的意图是转换这段代码:
Public NotInheritable Class MyType
''' <summary>
''' Gets or sets the value.
''' </summary>
''' <value>The value.</value>
Public Property MyProperty As Integer
Get
Return Me._MyValue
End Get
Set(ByVal value As Integer)
If value < Me._MyValueMin Then
If Me._MyValueThrowRangeException Then
Throw New ArgumentOutOfRangeException("MyValue", Me._MyValueExceptionMessage)
End If
Me._MyValue = Me._MyValueMin
ElseIf value > Me._MyValueMax Then
If Me._MyValueThrowRangeException Then
Throw New ArgumentOutOfRangeException("MyValue", Me._MyValueExceptionMessage)
End If
Me._MyValue = Me._MyValueMax
Else
Me._MyValue = value
End If
End Set
End Property
Private _MyValue As Integer = 0I
Private _MyValueMin As Integer = 0I
Private _MyValueMax As Integer = 10I
Private _MyValueThrowRangeException As Boolean = True
Private _MyValueExceptionMessage As String = String.Format("The valid range is beetwen {0} and {1}",
Me._MyValueMin, Me._MyValueMax)
End Class
变成可重用和简化的东西,像这样:
Public NotInheritable Class MyType
''' <summary>
''' Gets or sets the value.
''' Valid range is between 0 and 10.
''' </summary>
''' <value>The value.</value>
<RangeAttribute(0, 10, ThrowRangeException:=False, ExceptionMessage:="")>
Public Property MyProperty As Integer
End Class
因此,为了完成这项任务,我已经开始编写属性,但由于文档或示例不足而未完成,然后我不知道如何在不手动添加的情况下评估属性的 setter 中的值上面代码属性中的getter/setter:
<AttributeUsage(AttributeTargets.Property Or
AttributeTargets.Parameter Or
AttributeTargets.ReturnValue Or
AttributeTargets.Field,
AllowMultiple:=False)>
Public Class RangeAttribute : Inherits Attribute
''' <summary>
''' Indicates the Minimum range value.
''' </summary>
Public Minimum As Single
''' <summary>
''' Indicates the Maximum range value.
''' </summary>
Public Maximum As Single
''' <summary>
''' Determines whether to throw an exception when the value is not in range.
''' </summary>
Public ThrowRangeException As Boolean
''' <summary>
''' Indicates the exception message to show when the value is not in range.
''' </summary>
Public ExceptionMessage As String
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class.
''' </summary>
''' <param name="Minimum">The minimum range value.</param>
''' <param name="Maximum">The maximum range value.</param>
Public Sub New(ByVal Minimum As Single,
ByVal Maximum As Single)
Me.New(Minimum, Maximum, ThrowRangeException:=False, ExceptionMessage:=String.Empty)
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class.
''' </summary>
''' <param name="Minimum">The minimum range value.</param>
''' <param name="Maximum">The maximum range value.</param>
''' <param name="ThrowRangeException">
''' Determines whether to throw an exception when the value is not in range.
''' </param>
Public Sub New(ByVal Minimum As Single,
ByVal Maximum As Single,
ByVal ThrowRangeException As Boolean,
Optional ByVal ExceptionMessage As String = "")
Me.Minimum = Minimum
Me.Maximum = Maximum
Me.ThrowRangeException = ThrowRangeException
If Not String.IsNullOrEmpty(ExceptionMessage) Then
Me.ExceptionMessage = ExceptionMessage
Else
Me.ExceptionMessage = String.Format("The valid range is beetwen {0} and {1}", Minimum, Maximum)
End If
End Sub
End Class
上面的属性代码将忽略不在范围内的值,我理解这是因为我没有评估任何内容,但我不知道该怎么做。
【问题讨论】:
-
Fody 做了类似的事情。它在编译时注入 INotifyPropertyChanged 代码。随意查看源代码,看看是否对您有帮助。
标签: .net vb.net winforms types attributes