Click here to Skip to main content
16,015,097 members

Survey Results

Do you use Attributes in your .NET development?   [Edit]

Survey period: 17 Oct 2011 to 24 Oct 2011

Attributes provide a neat way to hide the plumbing. Do you use and/or create attributes yourself?

OptionVotes% 
Extensively626.31
Often17017.31
Occasionally27928.41
Rarely19419.76
Never787.94
I am unfamiliar with Attributes in .NET19920.26



 
GeneralIt's awesome when you figure its power Pin
Fabio Franco21-Oct-11 2:17
professionalFabio Franco21-Oct-11 2:17 
GeneralRe: It's awesome when you figure its power Pin
Rakesh Meel23-Oct-11 19:48
professionalRakesh Meel23-Oct-11 19:48 
GeneralYes, When in my first week of new contract Pin
raju melveetilpurayil20-Oct-11 11:54
professionalraju melveetilpurayil20-Oct-11 11:54 
GeneralAttributes are the undead citizens of the .net code world. Pin
dave.dolan20-Oct-11 8:50
dave.dolan20-Oct-11 8:50 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 5:40
professionalFabio Franco21-Oct-11 5:40 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 5:46
dave.dolan21-Oct-11 5:46 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:07
professionalFabio Franco21-Oct-11 6:07 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 6:11
dave.dolan21-Oct-11 6:11 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:21
professionalFabio Franco21-Oct-11 6:21 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 6:27
dave.dolan21-Oct-11 6:27 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:43
professionalFabio Franco21-Oct-11 6:43 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 6:45
dave.dolan21-Oct-11 6:45 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:49
professionalFabio Franco21-Oct-11 6:49 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
PIEBALDconsult21-Oct-11 9:38
mvePIEBALDconsult21-Oct-11 9:38 
GeneralReflection + Attributes = FUN Pin
milkplus18-Oct-11 5:36
milkplus18-Oct-11 5:36 
GeneralRe: Reflection + Attributes = FUN Pin
Fabio Franco21-Oct-11 5:35
professionalFabio Franco21-Oct-11 5:35 
GeneralMostly with enumerations Pin
PIEBALDconsult17-Oct-11 18:13
mvePIEBALDconsult17-Oct-11 18:13 
GeneralRe: Mostly with enumerations Pin
ThatsAlok17-Oct-11 20:37
ThatsAlok17-Oct-11 20:37 
Generalwhat?? Pin
Dennis E White17-Oct-11 10:26
professionalDennis E White17-Oct-11 10:26 
GeneralRe: what?? Pin
CDP180217-Oct-11 11:27
CDP180217-Oct-11 11:27 
GeneralRe: what?? Pin
Dennis E White17-Oct-11 11:35
professionalDennis E White17-Oct-11 11:35 
GeneralRe: what?? Pin
Stonkie17-Oct-11 11:54
Stonkie17-Oct-11 11:54 
GeneralRe: what?? Pin
Edward Steward3-Dec-11 23:15
Edward Steward3-Dec-11 23:15 
Hi Dennis,

I've been using attributes extensively in nearly all my business objects for quite a while now and I find them to be most useful for general validation and integrity checks. I have a series of attributes that are purely business object related and these in turn help to reduce the repetitive nature of business objects and the general bloat of validation code.

Business object property validation[^]

An example of a custom attribute would be as follows:

VB
Option Strict Off
Option Explicit On

Imports System.Reflection

Namespace Attributes

    <AttributeUsage(AttributeTargets.Field Or AttributeTargets.Property)> Public Class NotEmptyAttribute
        Inherits attribBase

        Implements ITest

        Private mstrDependantPropName As String

        Public Sub New(Optional ByVal strDependantPropName As String = "")
            mstrDependantPropName = strDependantPropName
        End Sub

        Public Function GetRule() As String Implements ITest.GetRule
            Return "The value cannot be a zero length (empty) string"
        End Function

        Public Function TestCondition(ByVal Value As Object, ByRef cls As Object) As Boolean Implements ITest.TestCondition

            If Value Is Nothing Then Return True

            Dim lStr As String = CType(Value, String)

            If Not lStr.Equals(String.Empty) Then Return False

            '--------------------------------------------------------------------------------------

            If mstrDependantPropName.Equals(String.Empty) Then
                Return True
            End If

            '--------------------------------------------------------------------------------------

            Dim lStrPropType As String = String.Empty
            Dim lObjVal As Object = Nothing

            If SetLocalObjects(mstrDependantPropName, cls, lStrPropType, lObjVal) = False Then Return False

            '--------------------------------------------------------------------------------------

            Return TestDependant(lStrPropType, lObjVal)

        End Function

    End Class

End Namespace


As you can this attribute inherits from a base class:

VB
Option Strict Off
Option Explicit On

Imports System.Reflection

Namespace Attributes

    <AttributeUsage(AttributeTargets.Field Or AttributeTargets.Property)> Public Class attribBase
        Inherits System.Attribute

        Protected Overridable Function SetLocalObjects(ByVal propName As String, ByRef cls As Object, ByRef strPropType As String, ByRef objVal As Object) As Boolean

            Dim t As Type = cls.GetType
            Dim m As System.Reflection.MemberInfo() = t.GetMember(propName)

            If m.Length < 1 Then Return False

            Dim pi As PropertyInfo
            Dim fi As FieldInfo

            If TypeOf m(0) Is PropertyInfo Then
                pi = t.GetProperty(propName)
                strPropType = pi.PropertyType.ToString
                objVal = pi.GetValue(cls, Nothing)
            Else
                fi = t.GetField(propName)
                strPropType = fi.FieldType.ToString
                objVal = fi.GetValue(cls)
            End If

            Return True

        End Function

        Protected Overridable Function TestDependant(ByVal strPropType As String, ByVal objVal As Object) As Boolean

            Select Case strPropType
                Case GetType(Boolean).ToString
                    If CType(objVal, Boolean).Equals(False) Then Return False

                Case GetType(DateTime).ToString
                    If CType(objVal, DateTime).Equals(DateTime.MinValue) Then Return False

                Case GetType(String).ToString
                    If CType(objVal, String).Equals(String.Empty) Then Return False

                Case GetType(Guid).ToString
                    If CType(objVal, Guid).Equals(Guid.Empty) Then Return False

                Case GetType(Int32).ToString
                    If CType(objVal, Int32).Equals(CType(0, Int32)) Then Return False

                Case GetType(Int16).ToString
                    If CType(objVal, Int16).Equals(CType(0, Int16)) Then Return False

                Case GetType(Byte).ToString
                    If CType(objVal, Byte).Equals(CType(0, Byte)) Then Return False

            End Select

            Return True

        End Function

    End Class

End Namespace


Hope this helps...
Edward Steward
edwardsteward@optusnet.com.au

GeneralRe: what?? Pin
PIEBALDconsult17-Oct-11 18:09
mvePIEBALDconsult17-Oct-11 18:09 
GeneralRe: what?? Pin
Keith Barrow18-Oct-11 0:35
professionalKeith Barrow18-Oct-11 0:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.