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

发布于 2024-10-21 15:46:41 字数 2650 浏览 5 评论 0原文

我只想调用 Microsoft.Data 的 GenerateScript 方法来自 PowerShell 的 .Schema.ScriptDom.Sql.Sql100ScriptGenerator

#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

编辑:

当前版本是

$sql = 'select * from PowerShell'

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

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

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

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

$out

编辑:行中遇到错误

$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;

I just want to call the GenerateScript method of Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator from PowerShell.

#C

public void GenerateScript(
    IScriptFragment scriptFragment,
    out string script
)

I found this, but I do not get it to work

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

$sql = 'select * from PowerShell'

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

$out

this gives

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

Edit:

Current version is

$sql = 'select * from PowerShell'

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

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

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

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

$out

But I get an error in line

$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

I'm trying to convert

    IList<ParseError> errors;

    using (StringReader sr = new StringReader(inputScript))
    {
        fragment = parser.Parse(sr, out errors);
    }

Edit:

OK this works:

$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

and generates ( it removes the comment as intended )

SELECT *
FROM   PowerShell
WHERE  psRefnr = 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2024-10-28 15:46:41

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

您需要传递源自 的内容TSqlFragment。使用类似 TSql100Parser.ParseStatementList 的内容 方法,你将得到一个片段列表。

编辑:这篇博客文章与您的第二个错误有类似的问题。

I believe your issue is with your first parameter, which should be a IScriptFragment. You are passing a string.

You would need to pass something that derives from TSqlFragment. Using something like the TSql100Parser.ParseStatementList method, you will get a list of fragments.

EDIT: This blog post has a similar issue to your second error.

猥︴琐丶欲为 2024-10-28 15:46:41

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

Not entirely sure how this works with Powershell, but in normal C# you need to call an out parameter with the keyword "out" instead of the "ref" you have. Sorry if this is off base, but figured it might help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文