Truong Hong Thi wrote:
Anyone know a free vs.net 2003 plugin that is capable of generating
properties from private fields for C#? Thanks ahead.
Use my macro below and bind it to a keycombi, e.g cntrl-shift + n
say you have the field:
private string _foo;
you then type:
string foo
and on that line hit cntrl-shift-n
and you'll get
<summary>Gets / Sets Foo</summary>
public string Foo
{
get { return _foo;}
set { _foo = value;}
}
''' Creates a property.
''' format: type name has to be present at the current line.
''' Uses caMel casing.
Sub CreateProperty()
Dim typeName, propertyName, memberName, nameRead As String
Dim ts As TextSelection
Dim sb As New StringBuilder
Dim boolIsOpened As Boolean
Try
ts = DTE.ActiveDocument.Selection
If (ts.IsEmpty) Then
ts.SelectLine()
End If
typeName = (ts.Text.Split(" ")(0)).Trim()
nameRead = ((ts.Text.Split(" ")(1))).Trim()
propertyName = nameRead.Substring(0, 1).ToUpper &
nameRead.Substring(1)
memberName = "_" + nameRead.Substring(0, 1).ToLower() &
nameRead.Substring(1)
' actual property
sb.Append("/// <summary>" & Environment.NewLine)
sb.AppendFormat("/// Gets / sets {0}{1}", nameRead,
Environment.NewLine)
sb.Append("/// </summary>" & Environment.NewLine)
sb.Append("public " + typeName + " " + propertyName)
sb.Append(vbCrLf)
sb.Append("{")
sb.Append(vbCrLf)
' get
sb.Append("get")
sb.Append(vbCrLf)
sb.Append("{")
sb.Append(vbCrLf)
sb.AppendFormat("return {0};", memberName)
sb.Append(vbCrLf)
sb.Append("}")
sb.Append(vbCrLf)
' set
sb.Append("set")
sb.Append(vbCrLf)
sb.Append("{")
sb.Append(vbCrLf)
sb.AppendFormat("{0} = value;", memberName)
sb.Append(vbCrLf)
sb.Append("}")
sb.Append(vbCrLf)
sb.Append("}")
sb.Append(vbCrLf)
sb.Append(vbCrLf)
'Check to see if UndoContext object is already open.
If DTE.UndoContext.IsOpen = False Then
'Open the UndoContext object to track changes.
Call DTE.UndoContext.Open("CreateProperty " &
propertyName, False)
boolIsOpened = True
End If
' Replace the text
ts.Delete()
ts.Insert(sb.ToString(),
vsInsertFlags.vsInsertFlagsInsertAtStart)
Finally
'If UndoContext was already open, don't close it.
If boolIsOpened = True Then
'Close the UndoContext object to commit the changes.
DTE.UndoContext.Close()
' Format the Selection
ts.SmartFormat()
ts.MoveToPoint(ts.BottomPoint.CreateEditPoint())
ts.LineUp()
End If
End Try
End Sub
Frans
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET:
http://www.llblgen.com
My .NET blog:
http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------