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

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
2
by: tvmaly | last post by:
Due to the restrictions I have at my host, I cannot use smtplib in my email cgi script. They gave me a script they use that calls nullmailer-inject. I am trying to figure out how to add the...
3
by: Robert Mark Bram | last post by:
Howdy all! When do I call something a property and when do I call something an attribute? It has me a little confused at the moment! Any advice would be most appreciated! Rob
4
by: Justine | last post by:
Hi All, I would like to know where & when exactly we would have a need for attributes. A realistic example. Thanz in Advance, Justine
2
by: Harry F. Harrison | last post by:
I get 2 compile errors on assembly attributes after creating a custom attribute. If I comment out the attribute, the errors go away. I don't get it because my attribute specifies class usage,...
4
by: Theo | last post by:
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....
5
by: Joel Andres Granados | last post by:
Hi list: I have googled quite a bit on this matter and I can't seem to find what I need (I think Im just looking where I'm not suppose to :). I'm working with code that is not of my authorship...
1
by: =?Utf-8?B?SkhpbGJDUk0=?= | last post by:
I am trying to write a httpmodule to inject javascript includes on some of the aspx pages we have. For instance, I want to inject into the resulting IIS response something like... ...
2
by: Nathan Sokalski | last post by:
I am attempting to create icons for controls I have created using VB.NET by using the System.Drawing.ToolboxBitmap attribute. I have managed to do this in C# by specifying the path to the *.ico...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.