| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Reflection; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Metadata |
| | | 10 | | { |
| | | 11 | | // Recognizing types emitted by the F# compiler requires consuming APIs from the FSharp.Core runtime library. |
| | | 12 | | // Every F# application ships with a copy of FSharp.Core, however it is not available statically to System.Text.Json |
| | | 13 | | // The following class uses reflection to access the relevant APIs required to detect the various F# types we are lo |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Proxy class used to access FSharp.Core metadata and reflection APIs that are not statically available to System. |
| | | 17 | | /// </summary> |
| | | 18 | | internal sealed class FSharpCoreReflectionProxy |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// The various categories of F# types that System.Text.Json supports. |
| | | 22 | | /// </summary> |
| | | 23 | | public enum FSharpKind |
| | | 24 | | { |
| | | 25 | | Unrecognized, |
| | | 26 | | Option, |
| | | 27 | | ValueOption, |
| | | 28 | | List, |
| | | 29 | | Set, |
| | | 30 | | Map, |
| | | 31 | | Record, |
| | | 32 | | Union |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | // Binding a struct getter method to a delegate requires that the struct parameter is passed byref. |
| | | 36 | | public delegate TResult StructGetter<TStruct, TResult>(ref TStruct @this) where TStruct : struct; |
| | | 37 | | |
| | | 38 | | public const string FSharpCoreUnreferencedCodeMessage = "Uses Reflection to access FSharp.Core components at run |
| | | 39 | | |
| | | 40 | | private static FSharpCoreReflectionProxy? s_singletonInstance; |
| | | 41 | | |
| | | 42 | | // Every type generated by the F# compiler is annotated with the CompilationMappingAttribute |
| | | 43 | | // containing all relevant metadata required to determine its kind: |
| | | 44 | | // https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-compilationmappingattribute.html#SourceConstr |
| | | 45 | | private const string CompilationMappingAttributeTypeName = "Microsoft.FSharp.Core.CompilationMappingAttribute"; |
| | | 46 | | private readonly Type _compilationMappingAttributeType; |
| | | 47 | | private readonly MethodInfo? _sourceConstructFlagsGetter; |
| | | 48 | | |
| | | 49 | | private readonly Type? _fsharpOptionType; |
| | | 50 | | private readonly Type? _fsharpValueOptionType; |
| | | 51 | | private readonly Type? _fsharpListType; |
| | | 52 | | private readonly Type? _fsharpSetType; |
| | | 53 | | private readonly Type? _fsharpMapType; |
| | | 54 | | |
| | | 55 | | private readonly MethodInfo? _fsharpListCtor; |
| | | 56 | | private readonly MethodInfo? _fsharpSetCtor; |
| | | 57 | | private readonly MethodInfo? _fsharpMapCtor; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Checks if the provided System.Type instance is emitted by the F# compiler. |
| | | 61 | | /// If true, also initializes the proxy singleton for future by other F# types. |
| | | 62 | | /// </summary> |
| | | 63 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 64 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 65 | | public static bool IsFSharpType(Type type) |
| | 2301 | 66 | | { |
| | 2301 | 67 | | if (s_singletonInstance is null) |
| | 2301 | 68 | | { |
| | 2301 | 69 | | if (GetFSharpCoreAssembly(type) is Assembly fsharpCoreAssembly) |
| | 0 | 70 | | { |
| | | 71 | | // Type is F# type, initialize the singleton instance. |
| | 0 | 72 | | s_singletonInstance ??= new FSharpCoreReflectionProxy(fsharpCoreAssembly); |
| | | 73 | | |
| | 0 | 74 | | return true; |
| | | 75 | | } |
| | | 76 | | |
| | 2301 | 77 | | return false; |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | return s_singletonInstance.GetFSharpCompilationMappingAttribute(type) is not null; |
| | 2301 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the singleton proxy instance; prerequires a successful IsFSharpType call for proxy initialization. |
| | | 85 | | /// </summary> |
| | | 86 | | public static FSharpCoreReflectionProxy Instance |
| | | 87 | | { |
| | | 88 | | get |
| | 0 | 89 | | { |
| | 0 | 90 | | Debug.Assert(s_singletonInstance is not null, "should be initialized via a successful IsFSharpType call. |
| | 0 | 91 | | return s_singletonInstance; |
| | 0 | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 96 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | 0 | 97 | | private FSharpCoreReflectionProxy(Assembly fsharpCoreAssembly) |
| | 0 | 98 | | { |
| | 0 | 99 | | Debug.Assert(fsharpCoreAssembly.GetName().Name == "FSharp.Core"); |
| | | 100 | | |
| | 0 | 101 | | Type compilationMappingAttributeType = fsharpCoreAssembly.GetType(CompilationMappingAttributeTypeName)!; |
| | 0 | 102 | | _sourceConstructFlagsGetter = compilationMappingAttributeType.GetMethod("get_SourceConstructFlags", BindingF |
| | 0 | 103 | | _compilationMappingAttributeType = compilationMappingAttributeType; |
| | | 104 | | |
| | 0 | 105 | | _fsharpOptionType = fsharpCoreAssembly.GetType("Microsoft.FSharp.Core.FSharpOption`1"); |
| | 0 | 106 | | _fsharpValueOptionType = fsharpCoreAssembly.GetType("Microsoft.FSharp.Core.FSharpValueOption`1"); |
| | 0 | 107 | | _fsharpListType = fsharpCoreAssembly.GetType("Microsoft.FSharp.Collections.FSharpList`1"); |
| | 0 | 108 | | _fsharpSetType = fsharpCoreAssembly.GetType("Microsoft.FSharp.Collections.FSharpSet`1"); |
| | 0 | 109 | | _fsharpMapType = fsharpCoreAssembly.GetType("Microsoft.FSharp.Collections.FSharpMap`2"); |
| | | 110 | | |
| | 0 | 111 | | _fsharpListCtor = fsharpCoreAssembly.GetType("Microsoft.FSharp.Collections.ListModule")?.GetMethod("OfSeq", |
| | 0 | 112 | | _fsharpSetCtor = fsharpCoreAssembly.GetType("Microsoft.FSharp.Collections.SetModule")?.GetMethod("OfSeq", Bi |
| | 0 | 113 | | _fsharpMapCtor = fsharpCoreAssembly.GetType("Microsoft.FSharp.Collections.MapModule")?.GetMethod("OfSeq", Bi |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 117 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 118 | | public FSharpKind DetectFSharpKind(Type type) |
| | 0 | 119 | | { |
| | 0 | 120 | | Attribute? compilationMappingAttribute = GetFSharpCompilationMappingAttribute(type); |
| | | 121 | | |
| | 0 | 122 | | if (compilationMappingAttribute is null) |
| | 0 | 123 | | { |
| | 0 | 124 | | return FSharpKind.Unrecognized; |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | if (type.IsGenericType) |
| | 0 | 128 | | { |
| | 0 | 129 | | Type genericType = type.GetGenericTypeDefinition(); |
| | 0 | 130 | | if (genericType == _fsharpOptionType) return FSharpKind.Option; |
| | 0 | 131 | | if (genericType == _fsharpValueOptionType) return FSharpKind.ValueOption; |
| | 0 | 132 | | if (genericType == _fsharpListType) return FSharpKind.List; |
| | 0 | 133 | | if (genericType == _fsharpSetType) return FSharpKind.Set; |
| | 0 | 134 | | if (genericType == _fsharpMapType) return FSharpKind.Map; |
| | 0 | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | return (GetSourceConstructFlags(compilationMappingAttribute) & SourceConstructFlags.KindMask) switch |
| | 0 | 138 | | { |
| | 0 | 139 | | SourceConstructFlags.RecordType => FSharpKind.Record, |
| | 0 | 140 | | SourceConstructFlags.SumType => FSharpKind.Union, |
| | 0 | 141 | | _ => FSharpKind.Unrecognized |
| | 0 | 142 | | }; |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 146 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 147 | | public Func<TFSharpOption, T> CreateFSharpOptionValueGetter<[DynamicallyAccessedMembers(DynamicallyAccessedMembe |
| | 0 | 148 | | { |
| | 0 | 149 | | Debug.Assert(typeof(TFSharpOption).GetGenericTypeDefinition() == _fsharpOptionType); |
| | 0 | 150 | | MethodInfo valueGetter = EnsureMemberExists(typeof(TFSharpOption).GetMethod("get_Value", BindingFlags.Public |
| | 0 | 151 | | return CreateDelegate<Func<TFSharpOption, T>>(valueGetter); |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 155 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 156 | | public Func<TElement?, TFSharpOption> CreateFSharpOptionSomeConstructor<[DynamicallyAccessedMembers(DynamicallyA |
| | 0 | 157 | | { |
| | 0 | 158 | | Debug.Assert(typeof(TFSharpOption).GetGenericTypeDefinition() == _fsharpOptionType); |
| | 0 | 159 | | MethodInfo methodInfo = EnsureMemberExists(typeof(TFSharpOption).GetMethod("Some", BindingFlags.Public | Bin |
| | 0 | 160 | | return CreateDelegate<Func<TElement?, TFSharpOption>>(methodInfo); |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 164 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 165 | | public StructGetter<TFSharpValueOption, TElement> CreateFSharpValueOptionValueGetter<[DynamicallyAccessedMembers |
| | | 166 | | where TFSharpValueOption : struct |
| | 0 | 167 | | { |
| | 0 | 168 | | Debug.Assert(typeof(TFSharpValueOption).GetGenericTypeDefinition() == _fsharpValueOptionType); |
| | 0 | 169 | | MethodInfo valueGetter = EnsureMemberExists(typeof(TFSharpValueOption).GetMethod("get_Value", BindingFlags.P |
| | 0 | 170 | | return CreateDelegate<StructGetter<TFSharpValueOption, TElement>>(valueGetter); |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 174 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 175 | | public Func<TElement?, TFSharpOption> CreateFSharpValueOptionSomeConstructor<[DynamicallyAccessedMembers(Dynamic |
| | 0 | 176 | | { |
| | 0 | 177 | | Debug.Assert(typeof(TFSharpOption).GetGenericTypeDefinition() == _fsharpValueOptionType); |
| | 0 | 178 | | MethodInfo methodInfo = EnsureMemberExists(typeof(TFSharpOption).GetMethod("Some", BindingFlags.Public | Bin |
| | 0 | 179 | | return CreateDelegate<Func<TElement?, TFSharpOption>>(methodInfo); |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 183 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 184 | | public Func<IEnumerable<TElement>, TFSharpList> CreateFSharpListConstructor<TFSharpList, TElement>() |
| | 0 | 185 | | { |
| | 0 | 186 | | Debug.Assert(typeof(TFSharpList).GetGenericTypeDefinition() == _fsharpListType); |
| | 0 | 187 | | return CreateDelegate<Func<IEnumerable<TElement>, TFSharpList>>(EnsureMemberExists(_fsharpListCtor, "Microso |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 191 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 192 | | public Func<IEnumerable<TElement>, TFSharpSet> CreateFSharpSetConstructor<TFSharpSet, TElement>() |
| | 0 | 193 | | { |
| | 0 | 194 | | Debug.Assert(typeof(TFSharpSet).GetGenericTypeDefinition() == _fsharpSetType); |
| | 0 | 195 | | return CreateDelegate<Func<IEnumerable<TElement>, TFSharpSet>>(EnsureMemberExists(_fsharpSetCtor, "Microsoft |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | [RequiresUnreferencedCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 199 | | [RequiresDynamicCode(FSharpCoreUnreferencedCodeMessage)] |
| | | 200 | | public Func<IEnumerable<Tuple<TKey, TValue>>, TFSharpMap> CreateFSharpMapConstructor<TFSharpMap, TKey, TValue>() |
| | 0 | 201 | | { |
| | 0 | 202 | | Debug.Assert(typeof(TFSharpMap).GetGenericTypeDefinition() == _fsharpMapType); |
| | 0 | 203 | | return CreateDelegate<Func<IEnumerable<Tuple<TKey, TValue>>, TFSharpMap>>(EnsureMemberExists(_fsharpMapCtor, |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | private Attribute? GetFSharpCompilationMappingAttribute(Type type) |
| | 0 | 207 | | { |
| | 0 | 208 | | object[] attributes = type.GetCustomAttributes(_compilationMappingAttributeType, inherit: true); |
| | 0 | 209 | | return attributes.Length == 0 ? null : (Attribute)attributes[0]; |
| | 0 | 210 | | } |
| | | 211 | | |
| | | 212 | | private SourceConstructFlags GetSourceConstructFlags(Attribute compilationMappingAttribute) |
| | 0 | 213 | | => _sourceConstructFlagsGetter is null ? SourceConstructFlags.None : (SourceConstructFlags)_sourceConstructF |
| | | 214 | | |
| | | 215 | | // If the provided type is generated by the F# compiler, returns the runtime FSharp.Core assembly. |
| | | 216 | | private static Assembly? GetFSharpCoreAssembly(Type type) |
| | 2301 | 217 | | { |
| | 20931 | 218 | | foreach (Attribute attr in type.GetCustomAttributes(inherit: true)) |
| | 7014 | 219 | | { |
| | 7014 | 220 | | Type attributeType = attr.GetType(); |
| | 7014 | 221 | | if (attributeType.FullName == CompilationMappingAttributeTypeName) |
| | 0 | 222 | | { |
| | 0 | 223 | | return attributeType.Assembly; |
| | | 224 | | } |
| | 7014 | 225 | | } |
| | | 226 | | |
| | 2301 | 227 | | return null; |
| | 2301 | 228 | | } |
| | | 229 | | |
| | | 230 | | private static TDelegate CreateDelegate<TDelegate>(MethodInfo methodInfo) where TDelegate : Delegate |
| | 0 | 231 | | => (TDelegate)Delegate.CreateDelegate(typeof(TDelegate), methodInfo, throwOnBindFailure: true)!; |
| | | 232 | | |
| | | 233 | | private static TMemberInfo EnsureMemberExists<TMemberInfo>(TMemberInfo? memberInfo, string memberName) where TMe |
| | 0 | 234 | | { |
| | 0 | 235 | | if (memberInfo is null) |
| | 0 | 236 | | { |
| | 0 | 237 | | ThrowHelper.ThrowMissingMemberException_MissingFSharpCoreMember(memberName); |
| | | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | return memberInfo; |
| | 0 | 241 | | } |
| | | 242 | | |
| | | 243 | | // Replicates the F# source construct flags enum |
| | | 244 | | // https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-sourceconstructflags.html |
| | | 245 | | private enum SourceConstructFlags |
| | | 246 | | { |
| | | 247 | | None = 0, |
| | | 248 | | SumType = 1, |
| | | 249 | | RecordType = 2, |
| | | 250 | | ObjectType = 3, |
| | | 251 | | Field = 4, |
| | | 252 | | Exception = 5, |
| | | 253 | | Closure = 6, |
| | | 254 | | Module = 7, |
| | | 255 | | UnionCase = 8, |
| | | 256 | | Value = 9, |
| | | 257 | | KindMask = 31, |
| | | 258 | | NonPublicRepresentation = 32 |
| | | 259 | | } |
| | | 260 | | } |
| | | 261 | | } |