Un aspecto para utilizar con la librería Postsharp, para difundir un poquito la programación orientada a aspectos (AOP).
Este aspecto en particular sirve para definir un valor mínimo y máximo para un miembro público de una class (Ej: Una propiedad Byte, Short, Integer, Long, etc...),
con esto nos aseguramos de que el valor asignado nunca supere el máximo ...ni el mínimo.
Hay bastante repetición de código ya que al parecer la Class no se puede hacer genérica.
Ejemplo de uso:
Código fuente:
Este aspecto en particular sirve para definir un valor mínimo y máximo para un miembro público de una class (Ej: Una propiedad Byte, Short, Integer, Long, etc...),
con esto nos aseguramos de que el valor asignado nunca supere el máximo ...ni el mínimo.
Hay bastante repetición de código ya que al parecer la Class no se puede hacer genérica.
Ejemplo de uso:
Código (vbnet) [Seleccionar]
Imports PostSharp.Aspects
Public Class MyClass
<RangeAttribute(0S, SByte.MaxValue)>
Dim sByteValue As SByte
<RangeAttribute(0S, Byte.MaxValue)>
Dim ByteValue As Byte
<RangeAttribute(0S, Short.MaxValue)>
Dim Int16Value As Short
<RangeAttribute(0US, UShort.MaxValue)>
Dim UInt16Value As UShort
<RangeAttribute(0I, Integer.MaxValue)>
Dim Int32Value As Integer
<RangeAttribute(0UI, UInteger.MaxValue)>
Dim UInt32Value As UInteger
<RangeAttribute(0L, Long.MaxValue)>
Dim Int64Value As Long
<RangeAttribute(0UL, ULong.MaxValue)>
Dim UInt64Value As ULong
<RangeAttribute(0.0F, Single.MaxValue)>
Dim SglValue As Single
<RangeAttribute(0.0R, Double.MaxValue)>
Dim DblValue As Double
End Class
Código fuente:
Código (vbnet) [Seleccionar]
' ***********************************************************************
' Author : Elektro
' Modified : 07-June-2015
' ***********************************************************************
' <copyright file="RangeAttribute.vb" company="Elektro Studios">
' Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************
#Region " Usage Examples "
'Imports PostSharp.Aspects
'
'Public Class Myclass
'
' <RangeAttribute(0S, SByte.MaxValue)>
' Dim sByteValue As SByte
'
' <RangeAttribute(0S, Byte.MaxValue)>
' Dim ByteValue As Byte
'
' <RangeAttribute(0S, Short.MaxValue)>
' Dim Int16Value As Short
'
' <RangeAttribute(0US, UShort.MaxValue)>
' Dim UInt16Value As UShort
'
' <RangeAttribute(0I, Integer.MaxValue)>
' Dim Int32Value As Integer
'
' <RangeAttribute(0UI, UInteger.MaxValue)>
' Dim UInt32Value As UInteger
'
' <RangeAttribute(0L, Long.MaxValue)>
' Dim Int64Value As Long
'
' <RangeAttribute(0UL, ULong.MaxValue)>
' Dim UInt64Value As ULong
'
' <RangeAttribute(0.0F, Single.MaxValue)>
' Dim SglValue As Single
'
' <RangeAttribute(0.0R, Double.MaxValue)>
' Dim DblValue As Double
'
'End Class
#End Region
#Region " Imports "
Imports PostSharp.Aspects
#End Region
#Region " Range Attribute "
''' <summary>
''' Aspect that when applied to a property, defines its minimum and maximum value.
''' </summary>
<Serializable>
Public Class RangeAttribute : Inherits LocationInterceptionAspect
#Region " Properties "
''' <summary>
''' Gets or sets the minimum value.
''' </summary>
Private Property Min As Object
''' <summary>
''' Gets or sets the maximum value.
''' </summary>
Private Property Max As Object
#End Region
#Region " Constructors "
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="SByte"/> datatype.
''' </summary>
''' <param name="minInt8">The minimum <see cref="SByte"/> value.</param>
''' <param name="maxInt8">The maximum <see cref="SByte"/> value.</param>
Public Sub New(ByVal minInt8 As SByte, ByVal maxInt8 As SByte)
Me.Min = minInt8
Me.Max = maxInt8
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="Byte"/> datatype.
''' </summary>
''' <param name="minUInt8">The minimum <see cref="Byte"/> value.</param>
''' <param name="maxUInt8">The maximum <see cref="Byte"/> value.</param>
Public Sub New(ByVal minUInt8 As Byte, ByVal maxUInt8 As Byte)
Me.Min = minUInt8
Me.Max = maxUInt8
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="Short"/> datatype.
''' </summary>
''' <param name="minInt16">The minimum <see cref="Short"/> value.</param>
''' <param name="maxInt16">The maximum <see cref="Short"/> value.</param>
Public Sub New(ByVal minInt16 As Short, ByVal maxInt16 As Short)
Me.Min = minInt16
Me.Max = maxInt16
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="UShort"/> datatype.
''' </summary>
''' <param name="minUInt16">The minimum <see cref="UShort"/> value.</param>
''' <param name="maxUInt16">The maximum <see cref="UShort"/> value.</param>
Public Sub New(ByVal minUInt16 As UShort, ByVal maxUInt16 As UShort)
Me.Min = minUInt16
Me.Max = maxUInt16
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="Integer"/> datatype.
''' </summary>
''' <param name="minInt32">The minimum <see cref="Integer"/> value.</param>
''' <param name="maxInt32">The maximum <see cref="Integer"/> value.</param>
Public Sub New(ByVal minInt32 As Integer, ByVal maxInt32 As Integer)
Me.Min = minInt32
Me.Max = maxInt32
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="UInteger"/> datatype.
''' </summary>
''' <param name="minUInt32">The minimum <see cref="UInteger"/> value.</param>
''' <param name="maxUInt32">The maximum <see cref="UInteger"/> value.</param>
Public Sub New(ByVal minUInt32 As UInteger, ByVal maxUInt32 As UInteger)
Me.Min = minUInt32
Me.Max = maxUInt32
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="Long"/> datatype.
''' </summary>
''' <param name="minInt64">The minimum <see cref="Long"/> value.</param>
''' <param name="maxInt64">The maximum <see cref="Long"/> value.</param>
Public Sub New(ByVal minInt64 As Long, ByVal maxInt64 As Long)
Me.Min = minInt64
Me.Max = maxInt64
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="ULong"/> datatype.
''' </summary>
''' <param name="minUInt64">The minimum <see cref="ULong"/> value.</param>
''' <param name="maxUInt64">The maximum <see cref="ULong"/> value.</param>
Public Sub New(ByVal minUInt64 As ULong, ByVal maxUInt64 As ULong)
Me.Min = minUInt64
Me.Max = maxUInt64
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="Single"/> datatype.
''' </summary>
''' <param name="minSingle">The minimum <see cref="Single"/> value.</param>
''' <param name="maxSingle">The maximum <see cref="Single"/> value.</param>
Public Sub New(ByVal minSingle As Single, ByVal maxSingle As Single)
Me.Min = minSingle
Me.Max = maxSingle
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="RangeAttribute"/> class for <see cref="Double"/> datatype.
''' </summary>
''' <param name="minDouble">The minimum <see cref="Double"/> value.</param>
''' <param name="maxDouble">The maximum <see cref="Double"/> value.</param>
Public Sub New(ByVal minDouble As Double, ByVal maxDouble As Double)
Me.Min = minDouble
Me.Max = maxDouble
End Sub
''' <summary>
''' Prevents a default instance of the <see cref="RangeAttribute"/> class from being created.
''' </summary>
Private Sub New()
End Sub
#End Region
#Region " Methods "
''' <summary>
''' Method invoked <i>instead</i> of the <c>Set</c> semantic of the field or property to which the current aspect is applied,
''' i.e. when the value of this field or property is changed.
''' </summary>
''' <param name="args">Advice arguments.</param>
Public Overrides Sub OnSetValue(ByVal args As LocationInterceptionArgs)
Dim value As Object = args.Value
Select Case True
Case TypeOf value Is SByte
If DirectCast(value, SByte) < CSByte(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, SByte) > CSByte(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CSByte(value))
Case TypeOf value Is Byte
If DirectCast(value, Byte) < CByte(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, Byte) > CByte(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CByte(value))
Case TypeOf value Is Short
If DirectCast(value, Short) < CShort(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, Short) > CShort(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CShort(value))
Case TypeOf value Is UShort
If DirectCast(value, UShort) < CUShort(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, UShort) > CUShort(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CUShort(value))
Case TypeOf value Is Integer
If DirectCast(value, Integer) < CInt(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, Integer) > CInt(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CInt(value))
Case TypeOf value Is UInteger
If DirectCast(value, UInteger) < CUInt(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, UInteger) > CUInt(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CUInt(value))
Case TypeOf value Is Long
If DirectCast(value, Long) < CLng(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, Long) > CLng(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CLng(value))
Case TypeOf value Is ULong
If DirectCast(value, ULong) < CULng(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, ULong) > CULng(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CULng(value))
Case TypeOf value Is Single
If DirectCast(value, Single) < CSng(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, Single) > CSng(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CSng(value))
Case TypeOf value Is Double
If DirectCast(value, Double) < CDbl(Me.Min) Then
value = Me.Min
ElseIf DirectCast(value, Double) > CDbl(Me.Max) Then
value = Me.Max
End If
args.SetNewValue(CDbl(value))
End Select
End Sub
#End Region
End Class
#End Region