cUpdate Custom attribut with reflection

  • Thread starter Marc Robitaille
  • Start date

M

Marc Robitaille

Hello,

I have this Field attribut class and a Client class that uses this Attribut
Class

<AttributeUsage(AttributeTargets.Property)> _
Public Class Field
Inherits Attribute
Private _FieldName As String
Private _Changed As Boolean

Public Sub New(ByVal changed As Boolean, ByVal fieldName As String)
_Changed = changed
_FieldName = fieldName
End Sub

Public Property FieldName() As String
Get
Return _FieldName
End Get
Set(ByVal value As String)
_FieldName = value
End Set
End Property
Public Property Changed() As Boolean
Get
Return _Changed
End Get
Set(ByVal value As Boolean)
_Changed = value
End Set
End Property
End Class

Public Class Client
Private _name As String
<Field(False, "name")> _
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
If value <> _name Then
Dim oType As Type = Me.GetType
Dim oPropertyInfo As PropertyInfo =
oType.GetProperty("Name")
Dim oAllAttributes As Field()
oAllAttributes =
oPropertyInfo.GetCustomAttributes(GetType(Field), False)
For Each oAttribute As Field In oAllAttributes
oAttribute.Changed = True
Exit For

Next
_name = value
End If
End Set
End Property
End Class

In the click event of a button I have this

Dim x as new Client

x.Name = "My name"
x.Name = "Your name"

What I want to do is to use the Changed property of my Field attribut class
to tell me that the Name property has changed or not. With this, I want to
build the SQL update string with only the fields that the Changed attribut
is set to True. It doesn't work :-( The changed property is allways false.
What I do wrong? Is it possible to update the value of a property of an
attribut? if yes, how can I do this?

Thank you and hope that I am clear
Marc R.
 
Ad

Advertisements

T

Tom Shelton

Hello,

I have this Field attribut class and a Client class that uses this Attribut
Class

<AttributeUsage(AttributeTargets.Property)> _
Public Class Field
Inherits Attribute
Private _FieldName As String
Private _Changed As Boolean

Public Sub New(ByVal changed As Boolean, ByVal fieldName As String)
_Changed = changed
_FieldName = fieldName
End Sub

Public Property FieldName() As String
Get
Return _FieldName
End Get
Set(ByVal value As String)
_FieldName = value
End Set
End Property
Public Property Changed() As Boolean
Get
Return _Changed
End Get
Set(ByVal value As Boolean)
_Changed = value
End Set
End Property
End Class

Public Class Client
Private _name As String
<Field(False, "name")> _
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
If value <> _name Then
Dim oType As Type = Me.GetType
Dim oPropertyInfo As PropertyInfo =
oType.GetProperty("Name")
Dim oAllAttributes As Field()
oAllAttributes =
oPropertyInfo.GetCustomAttributes(GetType(Field), False)
For Each oAttribute As Field In oAllAttributes
oAttribute.Changed = True
Exit For

Next
_name = value
End If
End Set
End Property
End Class

In the click event of a button I have this

Dim x as new Client

x.Name = "My name"
x.Name = "Your name"

What I want to do is to use the Changed property of my Field attribut class
to tell me that the Name property has changed or not. With this, I want to
build the SQL update string with only the fields that the Changed attribut
is set to True. It doesn't work :-( The changed property is allways false.
What I do wrong? Is it possible to update the value of a property of an
attribut? if yes, how can I do this?

Thank you and hope that I am clear
Marc R.

Marc - I don't really think it is possible to change the value of an attribute
at runtime. I'm pretty sure that attributes are compile time thing and are
embeded in the metadata of the assembly. If someone knows differently, please
jump in and correct me.

Anyway, you most likely will have to implement an internal method of tracking
changes. Maybe an array of flags. You know when a property gets changed,
just hook it in the set method.

By the way - why don't you just use one of the O/R mapper's that are already
out there? NHibernate? WilsonO/R Mapper? LLBGen Pro? Really, you would be
saving your self a heck of a lot of work :)
 
M

Marc Robitaille

I do this because I want to learn :) What I have done so far, I have
something that work great to
create code but, I want to use every possible things that exist in .NET just
to learn. You can tell that I am creazy but I love to learn.

Thank you for your sugestion.
:)
 
Ad

Advertisements

T

Tom Shelton

I do this because I want to learn :) What I have done so far, I have
something that work great to
create code but, I want to use every possible things that exist in .NET just
to learn. You can tell that I am creazy but I love to learn.

Thank you for your sugestion.
:)

Ok, nothing wrong with wanting to learn. Done that many times myself.
Anyway, good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top