469,962 Members | 2,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,962 developers. It's quick & easy.

Attribute instantiation

Hi,
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.
How can i make this work?

Regards,
Theodore
Nov 20 '06 #1
4 1516
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

Nov 20 '06 #2
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

"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.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

Nov 20 '06 #3
Theo wrote:
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
>From what I have read, the Attribute is instantiated by the compiler
and then serialized to the target's meta data entry. That may be why
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

Nov 20 '06 #4
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/lib...attribute.aspx

Thanks again for looking into it.

Regards,
Theodore

"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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

Nov 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by johny smith | last post: by
7 posts views Thread by Hunter Hou | last post: by
12 posts views Thread by mlimber | last post: by
3 posts views Thread by Steven T. Hatton | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.