|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Attribute instantiationi created a custom attribute that accepts an argument in the constructor to fill a property. The constructor is supposed to be called when then attribute is set but that doesn't seem to happen. Sample code: <System.AttributeUsage(System.AttributeTargets.All, AllowMultiple:=True)> _ Public Class CustomAtt Inherits System.Attribute Public Sub New(ByVal Arg As String) Me.Arg = Arg if Arg = string.empty then throw new exception End Sub Private _Arg As String Public Property Arg() As String Get Return _Arg End Get Set(ByVal Value As String) _Arg = Value End Set End Property End Class <CustomAtt("something")> _ Public Class SomeClass Public Sub New() 'Do something End Sub End Class Running this in debug mode i noticed that the debuger does not go through the constructor of the attribute and there is not exception even if pass an empty string parameter. How can i make this work? Regards, Theodore Theo wrote:
Show quoteHide quote > i created a custom attribute that accepts an argument in the constructor to fill a property. The constructor is supposed to be called when then attribute is set but that doesn't seem to happen. Did you actually create an instance of the SomeClass somewhere? I> Sample code: > > <System.AttributeUsage(System.AttributeTargets.All, AllowMultiple:=True)> _ > Public Class CustomAtt > Inherits System.Attribute > > Public Sub New(ByVal Arg As String) > Me.Arg = Arg > if Arg = string.empty then throw new exception > End Sub > > Private _Arg As String > > Public Property Arg() As String > Get > Return _Arg > End Get > Set(ByVal Value As String) > _Arg = Value > End Set > End Property > End Class > > <CustomAtt("something")> _ > Public Class SomeClass > > Public Sub New() > 'Do something > End Sub > End Class > > > Running this in debug mode i noticed that the debuger does not go through the constructor of the attribute and there is not exception even if pass an empty string parameter. think your attribute gets instantiated when the class is, but I'm not 100% sure. By the way, it is standard convention that attribute classes acutally have the word 'Attribute' as part of the class name: Public Class CustomAttribute. I think then, that you can apply the attribute using only 'Custom' as in: <Custom("something")> _ Public Class SomeClass End Class Chris Hi Chris,
I did create an instance of the SomeClass and it didn't work. I tried an example I found in msdn library but didn't work. However, I noticed that the attribute was instantiating when I was attempting to list the assembly's attributes. Something triggered the instantiation that didn't occur before. Upon instantiation the attribute executes a method that succeeds under specific conditions and defines whether or not to continue loading the class. Therefore is crucial to me that the attribute instantiates properly. Thanks, Theodore Show quoteHide quote "Chris Dunaway" <dunaw***@gmail.com> wrote in message news:1164031269.626710.208920@j44g2000cwa.googlegroups.com... > Theo wrote: > >> i created a custom attribute that accepts an argument in the constructor >> to fill a property. The constructor is supposed to be called when then >> attribute is set but that doesn't seem to happen. >> Sample code: >> >> <System.AttributeUsage(System.AttributeTargets.All, >> AllowMultiple:=True)> _ >> Public Class CustomAtt >> Inherits System.Attribute >> >> Public Sub New(ByVal Arg As String) >> Me.Arg = Arg >> if Arg = string.empty then throw new exception >> End Sub >> >> Private _Arg As String >> >> Public Property Arg() As String >> Get >> Return _Arg >> End Get >> Set(ByVal Value As String) >> _Arg = Value >> End Set >> End Property >> End Class >> >> <CustomAtt("something")> _ >> Public Class SomeClass >> >> Public Sub New() >> 'Do something >> End Sub >> End Class >> >> >> Running this in debug mode i noticed that the debuger does not go through >> the constructor of the attribute and there is not exception even if pass >> an empty string parameter. > > Did you actually create an instance of the SomeClass somewhere? I > think your attribute gets instantiated when the class is, but I'm not > 100% sure. > > By the way, it is standard convention that attribute classes acutally > have the word 'Attribute' as part of the class name: Public Class > CustomAttribute. > > I think then, that you can apply the attribute using only 'Custom' as > in: > > <Custom("something")> _ > Public Class SomeClass > End Class > > Chris > Theo wrote:
> I did create an instance of the SomeClass and it didn't work. I tried an and then serialized to the target's meta data entry. That may be why> example I found in msdn library but didn't work. However, I noticed that the >From what I have read, the Attribute is instantiated by the compiler the breakpoint is not hit at runtime because the object has already been instantiated before the debugger is attached to the process. I think that in code that uses the class (SomeClass), if it calls GetCustomAttributes, looking for your custom attribute, then the attribute is instantiated from the parameters that were serialized to the meta data. The constructor would be called at that time. BTW: I found most of this information in Jeffrey Richter's book, "Applied Microsoft .Net Framework Programming". Regards, Chris Hi Chris,
thanks for the reply, what you actually say is accurate and I have confirmed it. What I don't understand is how do other attributes instantiate without calling GetCustomAttributes. I don't know if it is worth the trouble digging so much when you have to meet some serious deadlines. Microsoft claims that the attribute's constructor is called when the attribute is set or so it says in the example you can find in the following link: http://msdn2.microsoft.com/en-us/library/system.attribute.aspx Thanks again for looking into it. Regards, Theodore Show quoteHide quote "Chris Dunaway" <dunaw***@gmail.com> wrote in message news:1164039459.525840.323630@b28g2000cwb.googlegroups.com... > > I think that in code that uses the class (SomeClass), if it calls > GetCustomAttributes, looking for your custom attribute, then the > attribute is instantiated from the parameters that were serialized to > the meta data. The constructor would be called at that time. > > BTW: I found most of this information in Jeffrey Richter's book, > "Applied Microsoft .Net Framework Programming". > > Regards, > > Chris >
How to declare a sub class
thread dead lock Passing multiple arguments to the client-side JavaScript function in AJAX textbox help VB2005 Replace " character in string Save Changes in VB.NET 2005 with Access 2003 --- Not using any Binding Navigator DataGridView1 (VS2005) textbox.backgroundImage ConfigurationManager unrecognized vb2005 express textbox control |
|||||||||||||||||||||||