473,324 Members | 2,548 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,324 software developers and data experts.

Is it possible to create an attribute that generates a get/set property?

Dear all,

I'm new to C#, so forgive my stupid question.

In my question to avoid boilerplate code, I was wondering if
I could use attributes to generate some code for me.

For example

public class Foo
{
[GenerateProperty("Field")]
int m_field;
}

should be equivalent to

public class Foo
{
int m_field;

public int Field
{
get { return m_field; }
set { m_field = value; }
}
}

Is that possible?

Thanks in advance

Thorsten
Nov 17 '05 #1
8 1356
Thorsten,

No, it is not. You have to generate the code for the property yourself.
You can do this with code snippets in VS.NET 2005, I believe.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Thorsten Ottosen" <th***@wmdata.com> wrote in message
news:42***********************@news.sunsite.dk...
Dear all,

I'm new to C#, so forgive my stupid question.

In my question to avoid boilerplate code, I was wondering if
I could use attributes to generate some code for me.

For example

public class Foo
{
[GenerateProperty("Field")]
int m_field;
}

should be equivalent to

public class Foo
{
int m_field;

public int Field
{
get { return m_field; }
set { m_field = value; }
}
}

Is that possible?

Thanks in advance

Thorsten

Nov 17 '05 #2
hi,

No you cannot
In VS2005 you will be able to refactor it and create a property around it

All you have to do is wait :)

In the meantime I believe there r 3rd party tools for this.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Thorsten Ottosen" <th***@wmdata.com> wrote in message
news:42***********************@news.sunsite.dk...
Dear all,

I'm new to C#, so forgive my stupid question.

In my question to avoid boilerplate code, I was wondering if
I could use attributes to generate some code for me.

For example

public class Foo
{
[GenerateProperty("Field")]
int m_field;
}

should be equivalent to

public class Foo
{
int m_field;

public int Field
{
get { return m_field; }
set { m_field = value; }
}
}

Is that possible?

Thanks in advance

Thorsten

Nov 17 '05 #3

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@TK2MSFTNGP10.phx.gbl...
hi,

No you cannot
In VS2005 you will be able to refactor it and create a property around it
what do you mean?

That I simply manually implement the property?
All you have to do is wait :)


on what?

Thanks

Thorsten
Nov 17 '05 #4
"Thorsten Ottosen" <th***@wmdata.com> wrote in message
news:42***********************@news.sunsite.dk...

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
hi,

No you cannot
In VS2005 you will be able to refactor it and create a property around it


what do you mean?

That I simply manually implement the property?


For now, yes. Or find a third party tool that will do it for you (I believe
ReSharper has support for this among other things).
All you have to do is wait :)


on what?


On VS2005...

--
Adam Clauss
Nov 17 '05 #5
Hi,

"Thorsten Ottosen" <th***@wmdata.com> wrote in message
news:42***********************@news.sunsite.dk...

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
hi,

No you cannot
In VS2005 you will be able to refactor it and create a property around it


what do you mean?
That I simply manually implement the property?


No, I mean that with a right click over the member variable you will have a
"Refactor" option that you can select and the IDE will create the property
for you, if even this is too much for you ( it's for me ) there is a
keyboard shortcut for it.

All you have to do is wait :)


on what?


For VS 2005
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 17 '05 #6
Please feel free to use my macro designed for the purpose.

You should declare property backer fields using an underbar character and a
lower-case first letter like so:

Private int _myInt;

Running the macro on this line creates a property such as:

public int MyInt
{
get{return _myInt;}
set{_myInt=value;}
}

For my own preference I have tied this macro to the Ctrl+Shft+V (for
variable) key combination.

You can define all your fields and then just hit your chosen hot-key
combination repeatedly to turn all fields into public properties with the
correct get-set code.

Full macro code is after my signature.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
-------------------------------------------------------
Sub FeildToProperty()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstColumn)

DTE.ActiveDocument.Selection.EndOfLine(True)

DTE.ActiveDocument.Selection.Copy()

DTE.ActiveDocument.Selection.EndOfLine()

DTE.ActiveDocument.Selection.NewLine()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstColumn)

DTE.ActiveDocument.Selection.Paste()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstText)

DTE.ActiveDocument.Selection.WordRight(True)

Dim currtext As String = DTE.ActiveDocument.Selection.Text

If (currtext = "protected " Or currtext = "private " Or currtext = "public
") Then

DTE.ActiveDocument.Selection.Delete()

End If

DTE.ActiveDocument.Selection.EndOfLine()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstText)

DTE.ActiveDocument.Selection.Text = "public "

DTE.ActiveDocument.Selection.WordRight()

DTE.ActiveDocument.Selection.WordRight(True)

DTE.ActiveDocument.Selection.Copy()

DTE.ActiveDocument.Selection.CharLeft()

DTE.ActiveDocument.Selection.Delete()

DTE.ActiveDocument.Selection.CharRight(True)

DTE.ActiveDocument.Selection.ChangeCase(vsCaseOpti ons.vsCaseOptionsUppercase)

DTE.ActiveDocument.Selection.WordRight()

DTE.ActiveDocument.Selection.EndOfLine(True)

DTE.ActiveDocument.Selection.Delete()

DTE.ActiveDocument.Selection.NewLine()

DTE.ActiveDocument.Selection.Text = "{"

DTE.ActiveDocument.Selection.NewLine()

DTE.ActiveDocument.Selection.Text = "}"

DTE.ActiveDocument.Selection.NewLine()

DTE.ActiveDocument.Selection.LineUp(False, 2)

DTE.ActiveDocument.Selection.EndOfLine()

DTE.ActiveDocument.Selection.NewLine()

DTE.ActiveDocument.Selection.Text = "get{return "

DTE.ActiveDocument.Selection.Paste()

DTE.ActiveDocument.Selection.Text = ";}"

DTE.ActiveDocument.Selection.NewLine()

DTE.ActiveDocument.Selection.Text = "set{"

DTE.ActiveDocument.Selection.Paste()

DTE.ActiveDocument.Selection.Text = "=value;}"

DTE.ActiveDocument.Selection.LineDown(False, 3)

DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstText)

End Sub

-------------------------------------------------------


"Thorsten Ottosen" <th***@wmdata.com> wrote in message
news:42***********************@news.sunsite.dk...
Dear all,

I'm new to C#, so forgive my stupid question.

In my question to avoid boilerplate code, I was wondering if
I could use attributes to generate some code for me.

For example

public class Foo
{
[GenerateProperty("Field")]
int m_field;
}

should be equivalent to

public class Foo
{
int m_field;

public int Field
{
get { return m_field; }
set { m_field = value; }
}
}

Is that possible?

Thanks in advance

Thorsten

Nov 17 '05 #7

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:OB****************@TK2MSFTNGP15.phx.gbl...
Please feel free to use my macro designed for the purpose.

Thanks :-)

best regards

Thorsten
Nov 17 '05 #8
No, it is not possible. You must use a macro or add-in. My add-in (below)
provides features to create properties from scratch or to convert a field to
a property.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Thorsten Ottosen" <th***@wmdata.com> escribió en el mensaje
news:42***********************@news.sunsite.dk...
Dear all,

I'm new to C#, so forgive my stupid question.

In my question to avoid boilerplate code, I was wondering if
I could use attributes to generate some code for me.

For example

public class Foo
{
[GenerateProperty("Field")]
int m_field;
}

should be equivalent to

public class Foo
{
int m_field;

public int Field
{
get { return m_field; }
set { m_field = value; }
}
}

Is that possible?

Thanks in advance

Thorsten

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rolf Kemper | last post by:
Dear All, somehow I remember that such or similar question was discussed already somewhere. But I can't find it anymore. I have a template calling itself. As long it goes deeper into the...
24
by: Charles Crume | last post by:
Hello; My "index.htm" page has 3 frames (content, navigation bar, and logo). I set the "SRC" of the "logo" frame to a blank gif image and then want to change it's contents after the other two...
0
by: Horia Tudosie | last post by:
Using Visual Studio 2003 This is to report a series of bugs regarding the FlagsAttribute and (independently) the usage of interfaces in Web applications. Let’s declare xColors type like: ...
3
by: Tapas | last post by:
Hi, Generating a .cs file using CodeDom. It generates the class fine. But i have few queries about class generation. 1. How to create a protected member? By default it generates a private...
9
by: bob taylor | last post by:
Imagine I have two classes A and B. Class A has the function I, function J and Attribute K. Class B has the funcion X, function Y, Attribute Z. Is it possible to establish the following...
4
by: Fred Lazy | last post by:
Is is possible to retrieve the name of an .js file from within the .js file itself: For example, if the file is named "bla.js" and it is imported into an html document: <script...
0
by: PhilipDaniels | last post by:
I'm trying to create a tracing system for my application. I want to create a new type of Switch that is basically TraceSwitch with an extra attribute called "includeMethodName". So my application's...
9
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified,...
1
by: =?Utf-8?B?U3VtaXQgUmF3YXQgSW5kaWE=?= | last post by:
What I have seen in Asp.net applications that if i put two Asp buttons on the web form(Aspx page) it generates multiple name attribute of input tag on the client side. for eg. <asp:Button...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.