-
Notifications
You must be signed in to change notification settings - Fork 105
/
TypeExtensions.cs
125 lines (108 loc) · 4.51 KB
/
TypeExtensions.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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Reflection;
namespace WinRT
{
#if EMBED
internal
#else
public
#endif
static class TypeExtensions
{
private readonly static ConcurrentDictionary<Type, Type> HelperTypeCache = new ConcurrentDictionary<Type, Type>();
public static Type FindHelperType(this Type type)
{
return HelperTypeCache.GetOrAdd(type, (type) =>
{
if (typeof(Exception).IsAssignableFrom(type))
{
type = typeof(Exception);
}
Type customMapping = Projections.FindCustomHelperTypeMapping(type);
if (customMapping is object)
{
return customMapping;
}
string fullTypeName = type.FullName;
string ccwTypePrefix = "ABI.Impl.";
if (fullTypeName.StartsWith(ccwTypePrefix, StringComparison.Ordinal))
{
fullTypeName = fullTypeName.Substring(ccwTypePrefix.Length);
}
var helper = $"ABI.{fullTypeName}";
return type.Assembly.GetType(helper) ?? Type.GetType(helper);
});
}
public static Type GetHelperType(this Type type)
{
var helperType = type.FindHelperType();
if (helperType is object)
return helperType;
throw new InvalidOperationException($"Target type is not a projected type: {type.FullName}.");
}
public static Type GetGuidType(this Type type)
{
return type.IsDelegate() ? type.GetHelperType() : type;
}
public static Type FindVftblType(this Type helperType)
{
Type vftblType = helperType.GetNestedType("Vftbl");
if (vftblType is null)
{
return null;
}
if (helperType.IsGenericType && vftblType is object)
{
vftblType = vftblType.MakeGenericType(helperType.GetGenericArguments());
}
return vftblType;
}
internal static IntPtr GetAbiToProjectionVftblPtr(this Type helperType)
{
return (IntPtr)(helperType.FindVftblType() ?? helperType).GetField("AbiToProjectionVftablePtr", BindingFlags.Public | BindingFlags.Static).GetValue(null);
}
public static Type GetAbiType(this Type type)
{
return type.GetHelperType().GetMethod("GetAbi", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType;
}
public static Type GetMarshalerType(this Type type)
{
return type.GetHelperType().GetMethod("CreateMarshaler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType;
}
internal static Type GetMarshaler2Type(this Type type)
{
var helperType = type.GetHelperType();
var createMarshaler = helperType.GetMethod("CreateMarshaler2", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) ??
helperType.GetMethod("CreateMarshaler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
return createMarshaler.ReturnType;
}
internal static Type GetMarshalerArrayType(this Type type)
{
return type.GetHelperType().GetMethod("CreateMarshalerArray", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)?.ReturnType;
}
public static bool IsDelegate(this Type type)
{
return typeof(Delegate).IsAssignableFrom(type);
}
internal static bool IsTypeOfType(this Type type)
{
return typeof(Type).IsAssignableFrom(type);
}
public static Type GetRuntimeClassCCWType(this Type type)
{
return type.IsClass && !type.IsArray ? type.GetAuthoringMetadataType() : null;
}
private readonly static ConcurrentDictionary<Type, Type> AuthoringMetadataTypeCache = new ConcurrentDictionary<Type, Type>();
internal static Type GetAuthoringMetadataType(this Type type)
{
return AuthoringMetadataTypeCache.GetOrAdd(type, (type) =>
{
var ccwTypeName = $"ABI.Impl.{type.FullName}";
return type.Assembly.GetType(ccwTypeName, false) ?? Type.GetType(ccwTypeName, false);
});
}
}
}