I am trying to intercept property setter using postsharp LocationInterceptionAspect to perform some validation before setting the value to private members. It works fine however if the value data type is not compatible with property data type it throws InvalidCastException and does not invoke OnSetValue. Is there any way I can generically catch this exception, nullify the value and then process with setter?
Property Validator Aspect:
<Serializable()>
Public Class PropertyValidatorAttribute
Inherits LocationInterceptionAspect
Public Overrides Sub OnSetValue(args As LocationInterceptionArgs)
'Perform validation here
args.ProceedSetValue()
End Sub
End Class
Class where aspect is used:
Public Enum MyEnum
A = 1
B = 2
End Enum
Public Class SampleClass
<PropertyValidator()>
Public Property SomeProperty As MyEnum
End Class
Main:
Sub Main()
Dim x As New SampleClass()
x.SomeProperty = "X"
End Sub