Skip to content

Is there a way to infer the resulting type of an Ast without evaluating it #21438

Answered by MartinGC94
Natr1x asked this question in Q&A
Discussion options

You must be logged in to vote

I don't think you can do this without reflection, so you may as well use the Type Inference API rather than trying to reimplement it. Here's an example of how you can do it:

using namespace System.Management.Automation
using namespace System.Management.Automation.Language
$Method = [TypeInferenceRuntimePermissions].Assembly.GetType('System.Management.Automation.AstTypeInference').GetMethods() | Where-Object -FilterScript {
    $Params = $_.GetParameters()
    $Params.Count -eq 2 -and
    $Params[0].ParameterType -eq [Ast] -and
    $Params[1].ParameterType -eq [TypeInferenceRuntimePermissions]
}
$DemoAst = {Get-ChildItem C:\}.Ast
$InferredTypes = $Method.Invoke($null, ($DemoAst, [TypeInfer…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Natr1x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants