This repository was archived by the owner on Jun 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathTypeGetter.cs
More file actions
198 lines (157 loc) · 7.32 KB
/
Copy pathTypeGetter.cs
File metadata and controls
198 lines (157 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Internal;
using Microsoft.PowerShell.Commands;
using OutGridView.Models;
namespace OutGridView.Cmdlet
{
public class TypeGetter
{
private PSCmdlet _cmdlet;
public TypeGetter(PSCmdlet cmdlet)
{
_cmdlet = cmdlet;
}
public FormatViewDefinition GetFormatViewDefinitionForObject(PSObject obj)
{
var typeName = obj.BaseObject.GetType().FullName;
var types = _cmdlet.InvokeCommand.InvokeScript(@"Microsoft.PowerShell.Utility\Get-FormatData " + typeName).ToList();
//No custom type definitions found - try the PowerShell specific format data
if (types == null || types.Count == 0)
{
types = _cmdlet.InvokeCommand
.InvokeScript(@"Microsoft.PowerShell.Utility\Get-FormatData -PowerShellVersion $PSVersionTable.PSVersion " + typeName).ToList();
if (types == null || types.Count == 0)
{
return null;
}
}
var extendedTypeDefinition = types[0].BaseObject as ExtendedTypeDefinition;
return extendedTypeDefinition.FormatViewDefinition[0];
}
public static DataTableRow CastObjectToDataTableRow(PSObject ps, List<DataTableColumn> dataColumns, int objectIndex)
{
Dictionary<string, IValue> valuePairs = new Dictionary<string, IValue>();
foreach (var dataColumn in dataColumns)
{
var expression = new PSPropertyExpression(ScriptBlock.Create(dataColumn.PropertyScriptAccessor));
var result = expression.GetValues(ps).FirstOrDefault().Result;
var stringValue = result?.ToString() ?? String.Empty;
var isDecimal = decimal.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var decimalValue);
if (isDecimal)
{
valuePairs[dataColumn.ToString()] = new DecimalValue { DisplayValue = stringValue, SortValue = decimalValue };
}
else
{
var stringDecorated = new StringDecorated(stringValue);
valuePairs[dataColumn.ToString()] = new StringValue { DisplayValue = stringDecorated.ToString(OutputRendering.PlainText) };
}
}
return new DataTableRow(valuePairs, objectIndex);
}
private static void SetTypesOnDataColumns(List<DataTableRow> dataTableRows, List<DataTableColumn> dataTableColumns)
{
var dataRows = dataTableRows.Select(x => x.Values);
foreach (var dataColumn in dataTableColumns)
{
dataColumn.StringType = typeof(decimal).FullName;
}
//If every value in a column could be a decimal, assume that it is supposed to be a decimal
foreach (var dataRow in dataRows)
{
foreach (var dataColumn in dataTableColumns)
{
if (!(dataRow[dataColumn.ToString()] is DecimalValue))
{
dataColumn.StringType = typeof(string).FullName;
}
}
}
}
private List<DataTableColumn> GetDataColumnsForObject(List<PSObject> psObjects)
{
var dataColumns = new List<DataTableColumn>();
foreach (PSObject obj in psObjects)
{
var labels = new List<string>();
FormatViewDefinition fvd = GetFormatViewDefinitionForObject(obj);
var propertyAccessors = new List<string>();
if (fvd == null)
{
if (PSObjectIsPrimitive(obj))
{
labels = new List<string> { obj.BaseObject.GetType().Name };
propertyAccessors = new List<string> { "$_" };
}
else
{
labels = obj.Properties.Select(x => x.Name).ToList();
propertyAccessors = obj.Properties.Select(x => $"$_.\"{x.Name}\"").ToList();
}
}
else
{
var tableControl = fvd.Control as TableControl;
var definedColumnLabels = tableControl.Headers.Select(x => x.Label);
var displayEntries = tableControl.Rows[0].Columns.Select(x => x.DisplayEntry);
var propertyLabels = displayEntries.Select(x => x.Value);
//Use the TypeDefinition Label if availble otherwise just use the property name as a label
labels = definedColumnLabels.Zip(propertyLabels, (definedColumnLabel, propertyLabel) =>
{
if (String.IsNullOrEmpty(definedColumnLabel))
{
return propertyLabel;
}
return definedColumnLabel;
}).ToList();
propertyAccessors = displayEntries.Select(x =>
{
//If it's a propety access directly
if (x.ValueType == DisplayEntryValueType.Property)
{
return $"$_.\"{x.Value}\"";
}
//Otherwise return access script
return x.Value;
}).ToList();
}
for (var i = 0; i < labels.Count; i++)
{
dataColumns.Add(new DataTableColumn(labels[i], propertyAccessors[i]));
}
}
return dataColumns.Distinct().ToList();
}
public DataTable CastObjectsToTableView(List<PSObject> psObjects)
{
List<FormatViewDefinition> objectFormats = psObjects.Select(GetFormatViewDefinitionForObject).ToList();
var dataTableColumns = GetDataColumnsForObject(psObjects);
List<DataTableRow> dataTableRows = new List<DataTableRow>();
for (var i = 0; i < objectFormats.Count; i++)
{
var dataTableRow = CastObjectToDataTableRow(psObjects[i], dataTableColumns, i);
dataTableRows.Add(dataTableRow);
}
SetTypesOnDataColumns(dataTableRows, dataTableColumns);
return new DataTable(dataTableColumns, dataTableRows);
}
//Types that are condisidered primitives to PowerShell but not C#
private readonly static List<string> additionalPrimitiveTypes = new List<string> { "System.String",
"System.Decimal",
"System.IntPtr",
"System.Security.SecureString",
"System.Numerics.BigInteger"
};
private static bool PSObjectIsPrimitive(PSObject ps)
{
var psBaseType = ps.BaseObject.GetType();
return psBaseType.IsPrimitive || psBaseType.IsEnum || additionalPrimitiveTypes.Contains(psBaseType.FullName);
}
}
}