2012-10-19 31 views
6

में मैक्रोज़ की कमी को कैसे सुलझाना है मेरे संगठन में प्रमाणीकरण और प्रमाणीकरण प्राप्त करने के लिए हमारे कोड का पालन करना बेहद प्रतिबंधित और कठोर नियम है। पिछले दशक के लिए या तो हमने लगभग सौ वीएस मैक्रोज़ विकसित किए हैं जो कोड स्वरूपित करते हैं, टिप्पणियां ब्लॉक उत्पन्न करते हैं, स्टाइल नियमों को लागू करते हैं, आदिविजुअल स्टूडियो 2012

हमारे मैक्रोज़ आप कुछ माउस आंदोलनों को रिकॉर्ड नहीं करते हैं; वे सभी EnvDTE * वीएस स्वचालन वस्तुओं पर निर्भर करते हैं। वीएस 2012 मैक्रोज़ छोड़ने के साथ हम इस बात पर नुकसान पहुंचा रहे हैं कि हम टीम पर कठोर प्रभाव डाले बिना अपग्रेड करने में सक्षम होंगे या नहीं।

मुझे पता है कि माइक्रोसॉफ्ट की दिशा वीएस एडिनस मार्ग है और मैं उस मार्ग की जांच करने के लिए तैयार हूं लेकिन मुझे कोड नमूने या दस्तावेज़ीकरण को खोजने में परेशानी हो रही है कि वीएस एड-इन सक्रिय कोड फ़ाइल के साथ कैसे बातचीत कर सकता है विजुअल स्टूडियो में।

उदाहरण के लिए, यहाँ एक मैक्रो हम सब समय का उपयोग सभी तरीकों कि बिना क्रिया अपवाद

फेंक करने में सक्षम हैं के लिए हमारी कोशिश आवरण डिजाइन पैटर्न लागू होने वाला है
''' <summary> 
''' Wraps active method in Try* access wrappers. 
''' </summary> 
Sub InsertSingleMethodTryWrappers() 
    Dim textSelection As TextSelection 
    Dim codeElement As CodeElement 

    textSelection = DTE.ActiveWindow.Selection 

    DTE.UndoContext.Open("Generate Try Wrappers") 'Allow for single Undo operation to rollback all changes 

    Try 
     codeElement = textSelection.ActivePoint.CodeElement(vsCMElement.vsCMElementFunction) 
     If Not (codeElement Is Nothing) Then 
      Dim textSelection2 As TextSelection 
      Dim codeFunction As CodeFunction 
      'Dim codeFunction2 As CodeFunction2 
      Dim editPoint As EditPoint 
      Dim codeParameter As CodeParameter 
      Dim parameters As CodeElements 
      Dim codeElement2 As CodeElement 
      Dim isVirtual As Boolean = False 
      Dim strVirtual As String = String.Empty 
      Dim strTypeName As String = String.Empty 

      '' Cast the codeElement to codeFunction object 
      codeFunction = codeElement 

      '' Move cursor to the start of the method 
      textSelection.MoveToPoint(codeFunction.GetStartPoint(vsCMPart.vsCMPartHeader)) 

      '' Should be able to use codeFunction.Kind.ToString to retrieve the function type 
      '' vsCMFunctionVirtual if the method is virtual but there is a bug in the API 
      '' that returns vsCMFunctionFunction even if the function is virtual (C# parsing bug?) 
      '' 
      '' vsCMFunction Type 
      '' http://msdn.microsoft.com/en-us/library/envdte.vscmfunction(v=vs.80).aspx 
      '' 
      '' This frustrating bug means that we have to parse the header to determine if virtual 
      textSelection.EndOfLine(True) 
      If (textSelection.Text.IndexOf("virtual") > 0) Then 
       isVirtual = True 
       strVirtual = " virtual" 
      End If 
      textSelection.StartOfLine() 

      '' Try not to screw up comments and attributes 
      editPoint = GetNoneCommentOrAttribHeaderEditPoint(textSelection) 
      If editPoint Is Nothing Then 
       MsgBox("Could not find a line above the method that isn't a comment or attribute", _ 
         MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error") 
       Exit Sub 
      End If 

      '' Create an EditPoint to inject Try* methods 
      'editPoint = textSelection.TopPoint.CreateEditPoint() 
      'editPoint.LineUp() 'Move up 1 line 
      editPoint.EndOfLine() 'Go to end of line above signature 
      editPoint.Insert(Environment.NewLine) 'Insert blank line for cleanliness 
      editPoint.Insert(Environment.NewLine) 'Insert blank line for cleanliness 
      editPoint.LineUp() 'Move up 1 line 

      parameters = codeFunction.Parameters 

      Dim strAccess As String : strAccess = GetAccessModifierString(codeFunction.Access) 'Access Modifier 
      Dim strName As String : strName = codeElement.Name 'Member Name 
      Dim strType As String : strType = codeFunction.Type.AsString 'Type Name 

      '' Get the un-qualified object name 
      If (strType.IndexOf(".") > 0) Then 
       Dim arrType() As String = strType.Split(".") 
       strTypeName = arrType(arrType.Length - 1) 
      Else 
       strTypeName = strType 
      End If 

      ''' Create parameter type/name arrayList 
      Dim arrParams As System.Collections.ArrayList 
      arrParams = New System.Collections.ArrayList() 

      For Each codeElement2 In parameters 
       codeParameter = codeElement2 
       arrParams.Add(codeParameter.Type.AsString.Trim & " " & codeParameter.Name.Trim & ", ") 
      Next 

      Dim strParams As String 
      Dim strParamNames As String 

      '' Capture a string with parameter names and types and one just of names 
      For Each strParam As String In arrParams 
       strParams += strParam 
       strParamNames += strParam.Split(" ")(1) 
      Next 

      '' Trim excess comma for members of type void 
      If strType = "void" Then 
       If Not String.IsNullOrEmpty(strParams) Then 
        If strParams.TrimEnd.EndsWith(",") Then 
         strParams = strParams.TrimEnd() 
         strParams = strParams.Remove(strParams.Length - 1, 1) 
        End If 
       End If 
      End If 

      '' -- Try* swallow methods -- 
      '' we don't care what the exception is, we just want to know success or failure 
      Dim strTrySwallowSignature As String 
      Dim strTrySwallowBody As String 
      Dim strTryOutParams As String 
      Dim strOutDef As String 
      Dim strOutSig As String 

      '' Members of type 'void' get no out parameters 
      If Not strType = "void" Then 
       strTryOutParams = "out " & strTypeName & " outObjType" 
       strOutDef = "outObjType = null;" 
       strOutSig = " out outObjType," 
      End If 

      strTrySwallowSignature = vbTab & vbTab & strAccess & strVirtual & " bool Try" & strName & "(" & strParams & strTryOutParams & ")" 
      strTrySwallowBody = vbCrLf & vbTab & vbTab & "{" _ 
           & vbCrLf & vbTab & vbTab & vbTab & "Exception exception;" _ 
           & vbCrLf & vbTab & vbTab & vbTab & strOutDef _ 
           & vbCrLf & vbTab & vbTab & vbTab & "return Try" & strName & "(" & strParamNames & strOutSig & " out exception);" _ 
           & vbCrLf & vbTab & vbTab & "}" 


      '' -- Try* re-throw methods -- 
      '' We want to know success or failure as well as the exception if it failed 
      Dim strTryReThrowSignature As String 
      Dim strTryReThrowBody As String 

      '' Members of type 'void' only get out exception parameter 
      If Not strType = "void" Then 
       strTryOutParams = "out " & strTypeName & " outObjType, out Exception exception" 
       'strOutDef = "outObjType = new " & strTypeName & "();" 
       strOutDef = "outObjType = null;" 
      Else 
       strTryOutParams = "out Exception exception" 
      End If 

      strTryReThrowSignature = vbTab & vbTab & strAccess & strVirtual & " bool Try" & strName & "(" & strParams & strTryOutParams & ")" 
      strTryReThrowBody = vbCrLf & vbTab & vbTab & "{" _ 
           & vbCrLf & vbTab & vbTab & vbTab & "bool result = false;" _ 
           & vbCrLf & vbTab & vbTab & vbTab & "exception = null;" _ 
           & vbCrLf & vbTab & vbTab & vbTab & strOutDef _ 
           & vbCrLf & vbTab & vbTab & vbTab & "try" _ 
           & vbCrLf & vbTab & vbTab & vbTab & "{" _ 
           & vbCrLf & vbTab & vbTab & vbTab & vbTab & "// insert code here " _ 
           & vbCrLf & vbTab & vbTab & vbTab & vbTab & "//result = true; " _ 
           & vbCrLf & vbTab & vbTab & vbTab & vbTab & "throw new NotImplementedException();" _ 
           & vbCrLf & vbTab & vbTab & vbTab & "}" _ 
           & vbCrLf & vbTab & vbTab & vbTab & "catch (Exception e)" _ 
           & vbCrLf & vbTab & vbTab & vbTab & "{" _ 
           & vbCrLf & vbTab & vbTab & vbTab & vbTab & "exception = e;" _ 
           & vbCrLf & vbTab & vbTab & vbTab & "}" _ 
           & vbCrLf & vbTab & vbTab & vbTab & "return result;" _ 
           & vbCrLf & vbTab & vbTab & "}" 

      editPoint.Insert(strTrySwallowSignature) 
      editPoint.Insert(strTrySwallowBody) 
      editPoint.Insert(vbCrLf & vbCrLf) 
      editPoint.Insert(strTryReThrowSignature) 
      editPoint.Insert(strTryReThrowBody) 
      editPoint.Insert(vbCrLf) 

     End If 
    Catch Ex As Exception 
     MsgBox(Ex.Message) 
    Finally 
     DTE.UndoContext.Close() 
    End Try 
End Sub 

किसी ने मुझे करने के लिए कैसे एक वी.एस. 2012 को सीधे कर सकते हैं -इन सक्रिय/खुली कोड फ़ाइल (एनवीडीटीई * या 2012 के लिए जो भी ऑब्जेक्ट मॉडल उपलब्ध है) का उपयोग कर सकते हैं?

+0

समस्या हल हो गई ... मैक्रो ऑब्जेक्ट मॉडल वीएस ऑब्जेक्ट मॉडल का हिस्सा है, जो सीधे एडिन –

उत्तर

4

वैसे यह वास्तव में सरल साबित हुआ। मैक्रो ऑब्जेक्ट मॉडल को बदलता है वीएस मॉडल का हिस्सा है इसलिए कोई समस्या नहीं है।

http://msdn.microsoft.com/en-us/library/za2b25t3%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/ms228776.aspx

मैं होना चाहिए पता माइक्रोसॉफ्ट हमें मैक्रो-निर्भर डेवलपर्स ऐसे ही ठंड में बाहर छोड़ दिया नहीं होता!

+0

के भीतर उपलब्ध है vs22 में "मैक्रोज़" लिखने के साथ कैसे शुरू किया जाता है? लिंक या समान? – LosManos

+2

@ लॉसमेनोस देखें http://stackoverflow.com/questions/12027485/alternative-to-macros-in-visual-studio-2012 – Daemin

संबंधित मुद्दे