[RESOLVED] Change Custom Attribute Values At Run Time
I want to know how to change the values of my custom attributes.
The Attribute:
Code:
<AttributeUsage(AttributeTargets.Property, AllowMultiple:=True, Inherited:=True)> _
Public Class IndexAttribute
Inherits System.Attribute
Private _MaxLength As Integer
Public Property MaxLength() As Integer
Get
Return _MaxLength
End Get
Set(ByVal value As Integer)
_MaxLength = value
End Set
End Property
Private _NotNull As Boolean
Public Property NotNull() As Boolean
Get
Return _NotNull
End Get
Set(ByVal value As Boolean)
_NotNull = value
End Set
End Property
End Class
Attribute Setup:
Code:
Public Class Index
Private _Value As String
<IndexAttribute(MaxLength:=0, NotNull:=False)> _
Public Property Value() As String
Get
Return _Value
End Get
Set(ByVal value As String)
_Value = value
End Set
End Property
End Class
Attempt to Change Attribute Value at RunTime:
Code:
'--Get and Set the Custom Attributes for the Index object.
Dim _IndexAttribute As IndexAttribute
_IndexAttribute = CType(Attribute.GetCustomAttribute(_Index.GetType.GetProperty("Value"), _
GetType(IndexAttribute)), IndexAttribute)
_IndexAttribute.MaxLength = 102
Even though you are allowed to change the MaxLength, it doesn't stick for the _Index instance.
Re: Change Custom Attribute Values At Run Time
Attributes are metadata and are not instance based. Notice that you never refer to an instance of index to get the attribute instead you get the type of the instance. The Type is the blueprint if you will for what the structure is of the object but is not an instance of the object itself. It is at this level that the attribute lives and thus it does not persist values when changed. They are actually compiled into the Type.
One of the strengths of Attributes is that they allow you to query the metadata of an object without creating an instance. This allows you to find out about the object without creating it. This is very useful when creating a plug-in type scenerio.
Re: Change Custom Attribute Values At Run Time
Quote:
Originally Posted by Edneeis
Attributes are metadata and are not instance based. Notice that you never refer to an instance of index to get the attribute instead you get the type of the instance. The Type is the blueprint if you will for what the structure is of the object but is not an instance of the object itself. It is at this level that the attribute lives and thus it does not persist values when changed. They are actually compiled into the Type.
One of the strengths of Attributes is that they allow you to query the metadata of an object without creating an instance. This allows you to find out about the object without creating it. This is very useful when creating a plug-in type scenerio.
Interesting, I thought I was referring to an instance of the _Index object on this line of code, but I guess not! :lol:
Code:
CType(Attribute.GetCustomAttribute(_Index.GetType.....
Re: [RESOLVED] Change Custom Attribute Values At Run Time
Well after you call GetCustomAttribute you are dealing with an instance of the attribute but its a copy and not the same instance that is compiled as meta data for the type. At that point it is just like any other object but changes are not persisted back to the metadata.