473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

cUpdate Custom attribut with reflection

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.
Dec 6 '06 #1
3 1027
On 2006-12-06, Marc Robitaille <marc.mariewrote:
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 :)

--
Tom Shelton
Dec 6 '06 #2
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.
:-)

"Tom Shelton" <to*********@comcastXXXXXXX.neta écrit dans le message de
news: ef******************************@comcast.com...
On 2006-12-06, Marc Robitaille <marc.mariewrote:
>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 :)

--
Tom Shelton

Dec 7 '06 #3
On 2006-12-07, Marc Robitaille <marc.mariewrote:
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.

--
Tom Shelton
Dec 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: khaled Hajjar | last post by:
Hello, I have this template : <root> <categorie> <poste att = 'true'> ..... </poste> <poste att = 'false'> .....
9
by: Michael Schuerig | last post by:
I'd like to mark input elements with class "invalid" by appending something, say a "?". My understanding is that it should work like this: input.invalid:after { content:"?"; } However, at...
1
by: Sky Sigal | last post by:
(PS: Cross post from microsoft.pulic.dotnet.framework.aspnet.webcontrols) I've been looking lately for a way to keep the Properties panel for Controls 'clean'... My goal is to keep similar...
2
by: | last post by:
I'm writing web applications. I build and extend a lot of custom objects, and in the course of debugging my apps I invariably find myself writing a lot of junky code: Response.Write("Title: " +...
3
by: | last post by:
Hi all, I have a question on reflection Lets say I have a custom object called Address. Now, lets say I have a string variable that holds the name of a variable in the object such as...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
5
by: =?Utf-8?B?cGFnYXRlcw==?= | last post by:
Hello All, I am sure that I am just overlooking something, but here's something I can't quite get right... I want to be able to get the value of a parameter of an unknown custom attribute at...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.