This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 350
/
TypeTracker.cs
91 lines (77 loc) · 3.07 KB
/
TypeTracker.cs
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
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
namespace Microsoft.Scripting.Actions {
public abstract class TypeTracker : MemberTracker, IMembersList {
internal TypeTracker() {
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
public abstract Type Type {
get;
}
public abstract bool IsGenericType {
get;
}
public abstract bool IsPublic {
get;
}
#region IMembersList Members
public virtual IList<string> GetMemberNames() {
var members = new HashSet<string>();
GetMemberNames(Type, members);
return members.ToArray();
}
internal static void GetMemberNames(Type type, HashSet<string> result) {
foreach (Type ancestor in type.Ancestors()) {
foreach (MemberInfo mi in ancestor.GetDeclaredMembers()) {
if (!(mi is ConstructorInfo)) {
result.Add(mi.Name);
}
}
}
}
#endregion
/// <summary>
/// Enables implicit Type to TypeTracker conversions accross dynamic languages.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")]
public static explicit operator Type(TypeTracker tracker) {
TypeGroup tg = tracker as TypeGroup;
if (tg != null) {
Type res;
if (!tg.TryGetNonGenericType(out res)) {
throw ScriptingRuntimeHelpers.SimpleTypeError("expected non-generic type, got generic-only type");
}
return res;
}
return tracker.Type;
}
private static readonly Dictionary<Type, TypeTracker> _typeCache = new Dictionary<Type, TypeTracker>();
public static TypeTracker GetTypeTracker(Type type) {
TypeTracker res;
lock (_typeCache) {
if (!_typeCache.TryGetValue(type, out res)) {
_typeCache[type] = res = new NestedTypeTracker(type);
}
}
return res;
}
}
}