469,609 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Attribute to inject code for a property?


Hi,

Is there any why I could use attribute or something else
to instruct the compiler to inject a specific code for a property?

for example i have the following property and i would like that 3 lines of
code will be running
for the set function,note that i am not taking about code generation!

public virtual System.String TelephoneNumber
{
get
{
return m_TelephoneNumber;
}
set
{
//Line1
//Line2
//Line3+ m_TelephoneNumber
}
}

thanks in advance.
Nov 16 '05 #1
4 4271
what do you mean by injecting a specific code for a property? Can you be
more specific about your three lines?

"Julia" <co********@012.net.il> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...

Hi,

Is there any why I could use attribute or something else
to instruct the compiler to inject a specific code for a property?

for example i have the following property and i would like that 3 lines of
code will be running
for the set function,note that i am not taking about code generation!

public virtual System.String TelephoneNumber
{
get
{
return m_TelephoneNumber;
}
set
{
//Line1
//Line2
//Line3+ m_TelephoneNumber
}
}

thanks in advance.

Nov 16 '05 #2

"Julia" <co********@012.net.il> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...

Hi,

Is there any why I could use attribute or something else
to instruct the compiler to inject a specific code for a property?


There is no code generator support directly within the MS C# compiler. There
is a third party compiler that does something like this, but I can't
remember the name(or even if it works this way. It might simply have
parameter constraints). And, although I can't say for sure, this sounds like
AOP to me. You may be able to find an AOP framework for C# or perhaps go
totally overboard and use ContextBoundObject and write your own
hooks(http://msdn.microsoft.com/msdnmag/is.../default.aspx).
It is obnoxious, but it does work(in the 1.1 framework anyway, I can't say
firsthand about 2.0).

Most of the time you would use code generators(wonderful things, even better
with partial classes) or macros to achieve this. Even a pre-build step that
parses the code, sees the attribute, and injects it would be sufficent in
most cases.

The question really is why you aren't talking about code generation but you
are. I mean, injecting code is *still* generation, even if the generation
step happens to occur during compilation time. Is there some particular
reason you can't use some form of code generation?
Nov 16 '05 #3
I am talking about something like COM+ attribute
AutoComplete for example

Does'nt this attribute execute code before and after a method?

ha....i can see that the article talk about something similar so i am going
to read it now.
thanks.

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eg**************@TK2MSFTNGP15.phx.gbl...

"Julia" <co********@012.net.il> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...

Hi,

Is there any why I could use attribute or something else
to instruct the compiler to inject a specific code for a property?
There is no code generator support directly within the MS C# compiler.

There is a third party compiler that does something like this, but I can't
remember the name(or even if it works this way. It might simply have
parameter constraints). And, although I can't say for sure, this sounds like AOP to me. You may be able to find an AOP framework for C# or perhaps go
totally overboard and use ContextBoundObject and write your own
hooks(http://msdn.microsoft.com/msdnmag/is...nNET/default.a
spx). It is obnoxious, but it does work(in the 1.1 framework anyway, I can't say
firsthand about 2.0).

Most of the time you would use code generators(wonderful things, even better with partial classes) or macros to achieve this. Even a pre-build step that parses the code, sees the attribute, and injects it would be sufficent in
most cases.

The question really is why you aren't talking about code generation but you are. I mean, injecting code is *still* generation, even if the generation
step happens to occur during compilation time. Is there some particular
reason you can't use some form of code generation?

Nov 16 '05 #4

"Julia" <co********@012.net.il> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I am talking about something like COM+ attribute
AutoComplete for example

Does'nt this attribute execute code before and after a method?

ha....i can see that the article talk about something similar so i am
going
to read it now.


Yes, it looks like you are basically after the same concept.
AutoCompleteAttribute probably plugs into the COM+ architecture allowing
code to execute between calls. ContextBoundObject works in a similar
fashion. It isn't as straight forward(you may have to write a reflection
engine within a class level attribute that searches for your attribute on
the properties), but it should work.

This is definatly AOP, you should read about it sometime, ;).

It has its advantages, just not as many as some make it out to be.
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert Mark Bram | last post: by
4 posts views Thread by Justine | last post: by
2 posts views Thread by Harry F. Harrison | last post: by
4 posts views Thread by Theo | last post: by
5 posts views Thread by Joel Andres Granados | last post: by
1 post views Thread by =?Utf-8?B?SkhpbGJDUk0=?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.