forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebCmdletElementCollection.cs
More file actions
68 lines (60 loc) · 1.93 KB
/
Copy pathWebCmdletElementCollection.cs
File metadata and controls
68 lines (60 loc) · 1.93 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Collections.Generic;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// WebCmdletElementCollection for elements in html web responses
/// </summary>
public class WebCmdletElementCollection : ReadOnlyCollection<PSObject>
{
internal WebCmdletElementCollection(IList<PSObject> list)
: base(list)
{
}
/// <summary>
/// Finds the element with name or id
/// </summary>
/// <param name="nameOrId"></param>
/// <returns></returns>
public PSObject Find(string nameOrId)
{
// try Id first
PSObject result = FindById(nameOrId) ?? FindByName(nameOrId);
return (result);
}
/// <summary>
/// Finds the element by id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public PSObject FindById(string id)
{
return Find(id, true);
}
/// <summary>
/// Finds the element by name
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public PSObject FindByName(string name)
{
return Find(name, false);
}
private PSObject Find(string nameOrId, bool findById)
{
foreach (PSObject candidate in this)
{
var namePropInfo = candidate.Properties[(findById ? "id" : "name")];
if (namePropInfo != null && (string)namePropInfo.Value == nameOrId)
{
return candidate;
}
}
return null;
}
}
}