473,421 Members | 1,706 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,421 software developers and data experts.

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 1635
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ruud de Jong | last post by:
I have the situation where I need to construct the name of a static method, and then retrieve the corresponding function from a class object. I thought I could just use __getattribute__ for this...
7
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
7
by: Hunter Hou | last post by:
Hello, I'm trying one example of <<the C++ programming language>> Page 865 int f( int ); template< class T > T g( T t ) { return f( t ); } char c = g( 'a' ); ************ char f( char ); ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
6
by: Pierre Rouleau | last post by:
Hi all, Is there any reason that under Python you cannot instantiate the object class and create any attributes like you would be able for a normal class? Python 2.4.2 (#67, Sep 28 2005,...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
2
by: WittyGuy | last post by:
Hi My class looks something like this: class Base { public: Base () {} ~Base () {} // No virtual dtor in the base class private: };
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.