1

I'm building a custom scaffolder to use with MvcScaffolding. Trying to automate adding code to the beginning of a method, I came up with the following code:

$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")
    }
}

When I run my custom scaffolder, FindPattern fails with

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

As opposed to this macro, which runs just fine:

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

What am I doing wrong?

Călin Darie
  • 5,937
  • 1
  • 18
  • 13
  • Try `$editPoint.FindPattern([string]"\{")` or maybe `$editPoint.FindPattern('\{')`? I don't see where the type mismatch is coming from either, but maybe casting it explicitly will get you a different error. – Dan Fitch Apr 03 '12 at 20:33
  • Tried all combinations of casting/no cast, double/simple quotes and using a variable/string literal. When using a variable, `Write-Host $findWhat.GetType()` printed `System.String`. No luck, nothing changed. – Călin Darie Apr 04 '12 at 08:54

0 Answers0