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

  • Thread starter Thread starter Thorsten Ottosen
  • Start date Start date
T

Thorsten Ottosen

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
 
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.
 
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 ( .NET/ C# MVP ) said:
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
 
Thorsten Ottosen said:
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).

On VS2005...
 
Hi,



Thorsten Ottosen said:
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.


For VS 2005


cheers,
 
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(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)

DTE.ActiveDocument.Selection.EndOfLine(True)

DTE.ActiveDocument.Selection.Copy()

DTE.ActiveDocument.Selection.EndOfLine()

DTE.ActiveDocument.Selection.NewLine()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)

DTE.ActiveDocument.Selection.Paste()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.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(vsStartOfLineOptions.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(vsCaseOptions.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(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)

End Sub

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




Thorsten Ottosen said:
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
 
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
 
Back
Top