f3f7ee99627840780224f59c9c97db77ca5e919f
[platform/upstream/dotnet/runtime.git] /
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;
5 using System.Diagnostics.CodeAnalysis;
6 using System.Reflection;
7 using System.Collections.Generic;
8 using System.Runtime.InteropServices;
9
10 using Internal.Runtime.Augments;
11
12 using Internal.Reflection.Core;
13 using Internal.Reflection.Core.Execution;
14 using Internal.Reflection.Execution.PayForPlayExperience;
15 using Internal.Reflection.Extensions.NonPortable;
16
17 using System.Reflection.Runtime.General;
18
19 using Debug = System.Diagnostics.Debug;
20
21 namespace Internal.Reflection.Execution
22 {
23     //==========================================================================================================================
24     // This class provides various services down to System.Private.CoreLib. (Though we forward most or all of them directly up to Reflection.Core.)
25     //==========================================================================================================================
26     internal sealed class ReflectionExecutionDomainCallbacksImplementation : ReflectionExecutionDomainCallbacks
27     {
28         public ReflectionExecutionDomainCallbacksImplementation(ExecutionDomain executionDomain, ExecutionEnvironmentImplementation executionEnvironment)
29         {
30             _executionDomain = executionDomain;
31             _executionEnvironment = executionEnvironment;
32         }
33
34         public sealed override bool IsReflectionBlocked(RuntimeTypeHandle typeHandle)
35         {
36             return _executionEnvironment.IsReflectionBlocked(typeHandle);
37         }
38
39         //=======================================================================================
40         // This group of methods jointly service the Type.GetTypeFromHandle() path. The caller
41         // is responsible for analyzing the RuntimeTypeHandle to figure out which flavor to call.
42         //=======================================================================================
43         public sealed override Type GetNamedTypeForHandle(RuntimeTypeHandle typeHandle, bool isGenericTypeDefinition)
44         {
45             return _executionDomain.GetNamedTypeForHandle(typeHandle, isGenericTypeDefinition);
46         }
47
48         public sealed override Type GetArrayTypeForHandle(RuntimeTypeHandle typeHandle)
49         {
50             return _executionDomain.GetArrayTypeForHandle(typeHandle);
51         }
52
53         public sealed override Type GetMdArrayTypeForHandle(RuntimeTypeHandle typeHandle, int rank)
54         {
55             return _executionDomain.GetMdArrayTypeForHandle(typeHandle, rank);
56         }
57
58         public sealed override Type GetPointerTypeForHandle(RuntimeTypeHandle typeHandle)
59         {
60             return _executionDomain.GetPointerTypeForHandle(typeHandle);
61         }
62
63         public sealed override Type GetFunctionPointerTypeForHandle(RuntimeTypeHandle typeHandle)
64         {
65             return _executionDomain.GetFunctionPointerTypeForHandle(typeHandle);
66         }
67
68         public sealed override Type GetByRefTypeForHandle(RuntimeTypeHandle typeHandle)
69         {
70             return _executionDomain.GetByRefTypeForHandle(typeHandle);
71         }
72
73         public sealed override Type GetConstructedGenericTypeForHandle(RuntimeTypeHandle typeHandle)
74         {
75             return _executionDomain.GetConstructedGenericTypeForHandle(typeHandle);
76         }
77
78         //=======================================================================================
79         // Missing metadata exception support.
80         //=======================================================================================
81         public sealed override Exception CreateMissingMetadataException(Type pertainant)
82         {
83             return _executionDomain.CreateMissingMetadataException(pertainant);
84         }
85
86         // This is called from the ToString() helper of a RuntimeType that does not have full metadata.
87         // This helper makes a "best effort" to give the caller something better than "EETypePtr nnnnnnnnn".
88         public sealed override string GetBetterDiagnosticInfoIfAvailable(RuntimeTypeHandle runtimeTypeHandle)
89         {
90             return Type.GetTypeFromHandle(runtimeTypeHandle).ToDisplayStringIfAvailable();
91         }
92
93         public sealed override MethodBase GetMethodBaseFromStartAddressIfAvailable(IntPtr methodStartAddress)
94         {
95             RuntimeTypeHandle declaringTypeHandle = default(RuntimeTypeHandle);
96             QMethodDefinition methodHandle;
97             if (!ReflectionExecution.ExecutionEnvironment.TryGetMethodForStartAddress(methodStartAddress,
98                 ref declaringTypeHandle, out methodHandle))
99             {
100                 return null;
101             }
102
103             // We don't use the type argument handles as we want the uninstantiated method info
104             return ReflectionCoreExecution.ExecutionDomain.GetMethod(declaringTypeHandle, methodHandle, genericMethodTypeArgumentHandles: null);
105         }
106
107         public sealed override Assembly GetAssemblyForHandle(RuntimeTypeHandle typeHandle)
108         {
109             return Type.GetTypeFromHandle(typeHandle).Assembly;
110         }
111
112         public sealed override IntPtr TryGetStaticClassConstructionContext(RuntimeTypeHandle runtimeTypeHandle)
113         {
114             return ExecutionEnvironmentImplementation.TryGetStaticClassConstructionContext(runtimeTypeHandle);
115         }
116
117         public sealed override RuntimeTypeHandle GetTypeHandleIfAvailable(Type type)
118         {
119             return _executionDomain.GetTypeHandleIfAvailable(type);
120         }
121
122         public sealed override bool SupportsReflection(Type type)
123         {
124             return _executionDomain.SupportsReflection(type);
125         }
126
127         public sealed override MethodInfo GetDelegateMethod(Delegate del)
128         {
129             return DelegateMethodInfoRetriever.GetDelegateMethodInfo(del);
130         }
131
132         public sealed override Exception GetExceptionForHR(int hr)
133         {
134             return Marshal.GetExceptionForHR(hr);
135         }
136
137         private ExecutionDomain _executionDomain;
138         private ExecutionEnvironmentImplementation _executionEnvironment;
139     }
140 }