|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
cUpdate Custom attribut with reflectionI 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. On 2006-12-06, Marc Robitaille <marc.marie> wrote:
Show quoteHide quote > Hello, Marc - I don't really think it is possible to change the value of an attribute> > 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. > > 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 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. :-) news: efqdnecRrPnQHuvYnZ2dnUVZ_r6dn***@comcast.com..."Tom Shelton" <tom_shel***@comcastXXXXXXX.net> a écrit dans le message de Show quoteHide quote > On 2006-12-06, Marc Robitaille <marc.marie> wrote: >> 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 On 2006-12-07, Marc Robitaille <marc.marie> wrote:
> I do this because I want to learn :-) What I have done so far, I have Ok, nothing wrong with wanting to learn. Done that many times myself.> 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. >:-) > Anyway, good luck. -- Tom Shelton
Using XPath Against A Node
Close a form after x amount of time HTML help files how to use table in a dataset as data source for combobox? Confusing Arraylist BinarySearch problem Re: Is VB.NET Popular??? Is there some way to tell the debugger not to stop at WndProc? Need some info about the Load event of Forms and Usercontrols Insert key press in text box How to Step back to caller in break mode |
|||||||||||||||||||||||