1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
5 using System.Diagnostics.CodeAnalysis;
6 using System.Reflection;
7 using System.Collections.Generic;
8 using System.Runtime.InteropServices;
10 using Internal.Runtime.Augments;
12 using Internal.Reflection.Core;
13 using Internal.Reflection.Core.Execution;
14 using Internal.Reflection.Execution.PayForPlayExperience;
15 using Internal.Reflection.Extensions.NonPortable;
17 using System.Reflection.Runtime.General;
19 using Debug = System.Diagnostics.Debug;
21 namespace Internal.Reflection.Execution
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
28 public ReflectionExecutionDomainCallbacksImplementation(ExecutionDomain executionDomain, ExecutionEnvironmentImplementation executionEnvironment)
30 _executionDomain = executionDomain;
31 _executionEnvironment = executionEnvironment;
34 public sealed override bool IsReflectionBlocked(RuntimeTypeHandle typeHandle)
36 return _executionEnvironment.IsReflectionBlocked(typeHandle);
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)
45 return _executionDomain.GetNamedTypeForHandle(typeHandle, isGenericTypeDefinition);
48 public sealed override Type GetArrayTypeForHandle(RuntimeTypeHandle typeHandle)
50 return _executionDomain.GetArrayTypeForHandle(typeHandle);
53 public sealed override Type GetMdArrayTypeForHandle(RuntimeTypeHandle typeHandle, int rank)
55 return _executionDomain.GetMdArrayTypeForHandle(typeHandle, rank);
58 public sealed override Type GetPointerTypeForHandle(RuntimeTypeHandle typeHandle)
60 return _executionDomain.GetPointerTypeForHandle(typeHandle);
63 public sealed override Type GetFunctionPointerTypeForHandle(RuntimeTypeHandle typeHandle)
65 return _executionDomain.GetFunctionPointerTypeForHandle(typeHandle);
68 public sealed override Type GetByRefTypeForHandle(RuntimeTypeHandle typeHandle)
70 return _executionDomain.GetByRefTypeForHandle(typeHandle);
73 public sealed override Type GetConstructedGenericTypeForHandle(RuntimeTypeHandle typeHandle)
75 return _executionDomain.GetConstructedGenericTypeForHandle(typeHandle);
78 //=======================================================================================
79 // Missing metadata exception support.
80 //=======================================================================================
81 public sealed override Exception CreateMissingMetadataException(Type pertainant)
83 return _executionDomain.CreateMissingMetadataException(pertainant);
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)
90 return Type.GetTypeFromHandle(runtimeTypeHandle).ToDisplayStringIfAvailable();
93 public sealed override MethodBase GetMethodBaseFromStartAddressIfAvailable(IntPtr methodStartAddress)
95 RuntimeTypeHandle declaringTypeHandle = default(RuntimeTypeHandle);
96 QMethodDefinition methodHandle;
97 if (!ReflectionExecution.ExecutionEnvironment.TryGetMethodForStartAddress(methodStartAddress,
98 ref declaringTypeHandle, out methodHandle))
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);
107 public sealed override Assembly GetAssemblyForHandle(RuntimeTypeHandle typeHandle)
109 return Type.GetTypeFromHandle(typeHandle).Assembly;
112 public sealed override IntPtr TryGetStaticClassConstructionContext(RuntimeTypeHandle runtimeTypeHandle)
114 return ExecutionEnvironmentImplementation.TryGetStaticClassConstructionContext(runtimeTypeHandle);
117 public sealed override RuntimeTypeHandle GetTypeHandleIfAvailable(Type type)
119 return _executionDomain.GetTypeHandleIfAvailable(type);
122 public sealed override bool SupportsReflection(Type type)
124 return _executionDomain.SupportsReflection(type);
127 public sealed override MethodInfo GetDelegateMethod(Delegate del)
129 return DelegateMethodInfoRetriever.GetDelegateMethodInfo(del);
132 public sealed override Exception GetExceptionForHR(int hr)
134 return Marshal.GetExceptionForHR(hr);
137 private ExecutionDomain _executionDomain;
138 private ExecutionEnvironmentImplementation _executionEnvironment;