Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用输出参数调用.net方法?_.net_Powershell_Out - Fatal编程技术网

如何使用输出参数调用.net方法?

如何使用输出参数调用.net方法?,.net,powershell,out,.net,Powershell,Out,我只想调用来自PowerShell的GenerateScript方法 #C public void GenerateScript( IScriptFragment scriptFragment, out string script ) 我找到了,但我没有让它工作 $sg = new-object Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator $sql = 'select * from PowerShel

我只想调用来自PowerShell的GenerateScript方法

#C

public void GenerateScript(
    IScriptFragment scriptFragment,
    out string script
)
我找到了,但我没有让它工作

$sg = new-object  Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator

$sql = 'select * from PowerShell'

$out = ''
$sg.GenerateScript($sql, [ref] $out)

$out
这给

Cannot find an overload for "GenerateScript" and the argument count: "2".
At line:6 char:19
+ $sg.GenerateScript <<<< ($sql, [ref] $out)
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
但我在这条线上犯了个错误

$fragment = $parser.Parse($sr,([ref]$errors))



Cannot find an overload for "Parse" and the argument count: "2".
At line:11 char:26
+ $fragment = $parser.Parse <<<< ($sr,([ref]$errors))
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
并生成(按预期删除注释)


不完全确定这在Powershell中是如何工作的,但在普通C#中,您需要使用关键字“out”而不是现有的“ref”调用out参数。抱歉,如果这是不正确的,但我想这可能会有所帮助。

我相信您的问题在于第一个参数,它应该是一个IScriptFragment。你在传递一个字符串

您需要传递源于的内容。使用类似于该方法的方法,您将得到一个片段列表


编辑:这与您的第二个错误有类似的问题。

“但我无法让它工作”:请展开:您是否收到错误/警告?您得到的行为是否不是您期望的:您期望的是什么,您得到的是什么?问题可能与WMI相关,答案不适用。您再次传递了错误类型的参数。您正在为$errors传递一个要分析的字符串,而它需要一个IList。忘了提及,请尝试将$errors设置为$null。通过将其设置为“”,您将使其成为字符串。在powershell中,[ref]覆盖c#的out和ref。
$fragment = $parser.Parse($sr,([ref]$errors))



Cannot find an overload for "Parse" and the argument count: "2".
At line:11 char:26
+ $fragment = $parser.Parse <<<< ($sr,([ref]$errors))
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    IList<ParseError> errors;

    using (StringReader sr = new StringReader(inputScript))
    {
        fragment = parser.Parse(sr, out errors);
    }
$sql = @'
select * from PowerShell -- a comment
where psRefnr = 1
'@
$options = new-object Microsoft.Data.Schema.ScriptDom.Sql.SqlScriptGeneratorOptions

$sr = new-Object System.IO.StringReader($sql)

$sg =     new-object Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator($options)
$parser = new-object Microsoft.Data.Schema.ScriptDom.Sql.TSQL100parser($true)

$errors = $null
$fragment = $parser.Parse($sr,([ref]$errors))

$out = $null
$sg.GenerateScript($fragment,([ref]$out))

$out
SELECT *
FROM   PowerShell
WHERE  psRefnr = 1;