473,545 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Attribu teUsage(System. AttributeTarget s.All, AllowMultiple:= True)_
Public Class CustomAtt
Inherits System.Attribut e

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("som ething")_
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 1640
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.Attribu teUsage(System. AttributeTarget s.All, AllowMultiple:= True)_
Public Class CustomAtt
Inherits System.Attribut e

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("som ething")_
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("someth ing")_
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.goo glegroups.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.Attribu teUsage(System. AttributeTarget s.All,
AllowMultiple: =True)_
Public Class CustomAtt
Inherits System.Attribut e

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("som ething")_
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("someth ing")_
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
GetCustomAttrib utes, 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 GetCustomAttrib utes. 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.goo glegroups.com.. .
>
I think that in code that uses the class (SomeClass), if it calls
GetCustomAttrib utes, 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
3616
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 purpose. This works fine if I already have an instantiation of the class, but not when I try this on the class object directly. A bare bones...
7
1684
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
1698
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 ); Stroustrup said **** line is wrong because alternative resolution of f( t )
12
2601
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 an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with...
13
1686
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." here: http://jibbering.com/faq/faq_notes/closures.html
6
1745
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, 12:41:11) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a = object() >>> a.data = 1
3
3997
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 handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,...
2
1866
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
2479
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
7496
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7428
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7784
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6014
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5354
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.