This message was discovered on ASPFriends.com 'aspngcodegiveawayswap' list.
| Twomley, Jonathan |
-- Moved from [ngfx-io] to [aspngcodegiveawayswap] by Alex Lowe <Click here to reveal e-mail address> --
Does anyone have any macros that they would like to share with all of us?
|
|
| |
| | |
| |
| Andy Smith |
here's a macro I wrote which makes public properties in c#,vb, and = jscript. I don't know if this is the "best" way to do it, but it works. just put the cursor where you want the property, and start the macro.
Public Module MakeFoo
Sub MakeProperty() ' Get property info from user Dim propertyType As String =3D InputBox("Property Type?") If propertyType =3D "" Then Return
Dim propertyName As String =3D InputBox("Property Public Name?") If propertyName =3D "" Then Return
Dim propertyPrivateName As String =3D InputBox("Property Private = Name?", , GeneratePrivateName(propertyName)) If propertyPrivateName =3D "" Then Return
' Create a code generator Dim provider As CodeDomProvider Dim generator As ICodeGenerator Dim codeString As System.IO.StringWriter Dim options As New CodeGeneratorOptions()
If DTE.ActiveDocument.FullName.ToLower().EndsWith(".vb") Then provider =3D New Microsoft.VisualBasic.VBCodeProvider() ElseIf DTE.ActiveDocument.FullName.ToLower().EndsWith(".cs") = Then provider =3D New Microsoft.CSharp.CSharpCodeProvider() ElseIf DTE.ActiveDocument.FullName.ToLower().EndsWith(".js") = Then provider =3D New Microsoft.JScript.JScriptCodeProvider() Else Return End If
generator =3D provider.CreateGenerator()
' make sure the user entered valid variable names propertyName =3D generator.CreateValidIdentifier(propertyName) propertyPrivateName =3D = generator.CreateValidIdentifier(propertyPrivateName)
' create the public property Dim newProperty As New CodeMemberProperty() newProperty.Name =3D propertyName newProperty.Type =3D New CodeTypeReference(propertyType) newProperty.Attributes =3D MemberAttributes.Public newProperty.HasGet =3D True newProperty.HasSet =3D True
Dim getter As New CodeMethodReturnStatement() getter.Expression =3D New = CodeVariableReferenceExpression(propertyPrivateName) newProperty.GetStatements.Add(getter)
Dim setter As New CodeAssignStatement() setter.Left =3D New = CodeVariableReferenceExpression(propertyPrivateName) setter.Right =3D New CodePropertySetValueReferenceExpression() newProperty.SetStatements.Add(setter)
' create the private field Dim privateDeclaration As New = CodeVariableDeclarationStatement(propertyType, propertyPrivateName)
'Generate The Code And Add It To The Document
'HACK i wrap this with a class because the generator can't = generate from the property by itself Dim hackType As New CodeTypeDeclaration("hack") hackType.IsClass =3D True hackType.Members.Add(newProperty) codeString =3D New StringWriter() generator.GenerateCodeFromType(hackType, codeString, New = CodeGeneratorOptions()) Dim myCode As String =3D codeString.ToString() myCode =3D RemoveHackClass(myCode) 'ENDHACK
Dim mySelection As TextSelection =3D = DTE.ActiveDocument.Selection mySelection.Insert(myCode, = vsInsertFlags.vsInsertFlagsInsertAtEnd) mySelection.LineDown() mySelection.NewLine()
codeString =3D New StringWriter() generator.GenerateCodeFromStatement(privateDeclaration, = codeString, New CodeGeneratorOptions()) mySelection.Text =3D codeString.ToString()
End Sub
Private Function GeneratePrivateName(ByVal publicName As String) As = String If DTE.ActiveDocument.FullName.ToLower().EndsWith(".vb") Then Return "m" & publicName ElseIf DTE.ActiveDocument.FullName.ToLower().EndsWith(".cs") = Then Return publicName.Substring(0, 1).ToLower() & = publicName.Substring(1) ElseIf DTE.ActiveDocument.FullName.ToLower().EndsWith(".js") = Then Return "_" & publicName Else Return String.Empty End If End Function
Private Function RemoveHackClass(ByVal hack As String) As String Dim unHack As String =3D hack unHack =3D = unHack.Substring(unHack.IndexOf(System.Environment.NewLine)) unHack =3D unHack.Substring(0, = unHack.LastIndexOf(System.Environment.NewLine)) unHack =3D unHack.Substring(0, = unHack.LastIndexOf(System.Environment.NewLine)) Return unHack End Function
End Module
__ Andy Smith Keyboard Jockey #3a7-2.78.1
|
|
| |
|
|
|
|
|