-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathCachedMemberResultBase.cs
More file actions
82 lines (64 loc) · 2.12 KB
/
CachedMemberResultBase.cs
File metadata and controls
82 lines (64 loc) · 2.12 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace OutGridView.Cmdlet.TreeNodeCaching
{
abstract class CachedMemberResultBase : ICachedMemberResult
{
public object Value {get; protected set;}
public object Parent;
protected string Representation;
private List<CachedMemberResultElement> valueAsList;
public bool IsCollection => valueAsList != null;
public IReadOnlyCollection<CachedMemberResultElement> Elements => valueAsList?.AsReadOnly();
protected string ValueToString()
{
if (Value == null)
{
return "Null";
}
try
{
if (IsCollectionOfKnownTypeAndSize(out Type elementType, out int size))
{
return $"{elementType.Name}[{size}]";
}
}
catch (Exception)
{
return Value?.ToString();
}
return Value?.ToString();
}
private bool IsCollectionOfKnownTypeAndSize(out Type elementType, out int size)
{
elementType = null;
size = 0;
if (Value == null || Value is string)
{
return false;
}
if (Value is IEnumerable ienumerable)
{
var list = ienumerable.Cast<object>().ToList();
var types = list.Where(v => v != null).Select(v => v.GetType()).Distinct().ToArray();
if (types.Length == 1)
{
elementType = types[0];
size = list.Count;
valueAsList = list.Select((e, i) => new CachedMemberResultElement(e, i)).ToList();
return true;
}
}
return false;
}
public override string ToString()
{
return GetMemberName() + ": " + Representation;
}
protected abstract string GetMemberName();
}
}