我正在构建一个与MvcScaffolding一起使用的自定义脚手架。尝试自动将代码添加到方法的开头,我想出了以下代码:
$codeElement = Get-ProjectType "MyType"
foreach ($member in $codeElement.Members)
{
if ($member.Name -eq "CreateMappings")
{
$editPoint = $member.StartPoint.CreateEditPoint()
# here's where it's crashing
$editPoint.FindPattern("\{")
$editPoint.Insert("text")
}
}
当我运行自定义脚手架时,FindPattern
失败并带有
Exception calling "FindPattern" with "1" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
At Path\File.ps1:62 char:26
+ $editPoint.FindPattern <<<< ("\{")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
与这个运行得很好的宏相反:
Sub macro()
Dim objTD As TextDocument = DTE.ActiveDocument.Object("TextDocument")
Dim editPoint As EditPoint = objTD.StartPoint.CreateEditPoint()
If (editPoint.FindPattern("\{", vsFindOptions.vsFindOptionsRegularExpression)) Then
editPoint.CharRight()
editPoint.Insert("text")
End If
End Sub
我做错了什么?