Zav Hoz
Zav Hoz

Reputation: 11

Powershell parsing of a C# file to get values of constants

I need to parse some C# files to get the values of some constant variables. I know I can do something like

$input = Get-Content C:\somefile.cs ...

then loop over each line and do some text matching.

...but was wondering whether I can utilize some sort of a C# DOM object to get the values of constants?

Upvotes: 1

Views: 1231

Answers (1)

skiel85
skiel85

Reputation: 56

You can load the type dynamically from the powershell command line, and then evaluate the constant.

Example:

C:\somefile.cs contents:

public static class Foo
{
    public const string SOME_CONSTANT = "StackOverflow";
}

Powershell command line:

Add-Type -Path C:\somefile.cs
$constantValue = [Foo]::SOME_CONSTANT

Upvotes: 2

Related Questions