从PowerShell调用EnvDTE.TextSelection.FindPattern失败,类型不匹配
我正在建立一个定制的脚手架使用。尝试将代码自动添加到方法的开头,我产生了以下代码:从PowerShell调用EnvDTE.TextSelection.FindPattern失败,类型不匹配,powershell,com,envdte,asp.net-mvc-scaffolding,Powershell,Com,Envdte,Asp.net Mvc Scaffolding,我正在建立一个定制的脚手架使用。尝试将代码自动添加到方法的开头,我产生了以下代码: $codeElement = Get-ProjectType "MyType" foreach ($member in $codeElement.Members) { if ($member.Name -eq "CreateMappings") { $editPoint = $member.StartPoint.CreateEditPoint() # here'
$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
我做错了什么?试试$editPoint.FindPattern[string]\{或者$editPoint.FindPattern'\{'?我也不知道类型不匹配是从哪里来的,但可能显式强制转换会导致不同的错误。尝试了强制转换/不强制转换、双引号/简单引号以及使用变量/字符串文字的所有组合。使用变量时,请编写主机$findWhat.GetType printed System.string。运气不好,没有任何更改。
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