Skip to content

Commit

Permalink
Remove unnecessary abstraction layers
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas committed Mar 24, 2023
1 parent 0d90b3c commit 9f89797
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,6 @@ internal ExecutionDomain(ReflectionDomainSetup executionDomainSetup, ExecutionEn
ReflectionDomainSetup = executionDomainSetup;
}

//
// Retrieves a type by name. Helper to implement Type.GetType();
//
public Type GetType(string typeName, Func<AssemblyName, Assembly> assemblyResolver, Func<Assembly?, string, bool, Type?> typeResolver, bool throwOnError, bool ignoreCase, bool extensibleParser, string defaultAssemblyName)
{
return TypeNameParser.GetType(
typeName,
assemblyResolver: assemblyResolver,
typeResolver: typeResolver,
throwOnError: throwOnError,
ignoreCase: ignoreCase,
extensibleParser: extensibleParser,
defaultAssemblyName: defaultAssemblyName);
}

//
// Retrieves the MethodBase for a given method handle. Helper to implement Delegate.GetMethodInfo()
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ namespace Internal.Runtime.Augments
[System.Runtime.CompilerServices.ReflectionBlocked]
public abstract class ReflectionExecutionDomainCallbacks
{
// Api's that are exposed in System.Runtime but are really reflection apis.
public abstract Type GetType(string typeName, Func<AssemblyName, Assembly?>? assemblyResolver, Func<Assembly?, string, bool, Type?>? typeResolver, bool throwOnError, bool ignoreCase, bool extensibleParser, string defaultAssembly);

public abstract IntPtr TryGetStaticClassConstructionContext(RuntimeTypeHandle runtimeTypeHandle);

public abstract bool IsReflectionBlocked(RuntimeTypeHandle typeHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

using Internal.Runtime.Augments;
Expand All @@ -17,16 +18,14 @@ internal static class ReflectionHelpers
// a default assembly name.
public static Type GetType(string typeName, string callingAssemblyName, bool throwOnError, bool ignoreCase)
{
return RuntimeAugments.Callbacks.GetType(typeName, null, null,
throwOnError: throwOnError, ignoreCase: ignoreCase, extensibleParser: false, callingAssemblyName);
return TypeNameParser.GetType(typeName, throwOnError: throwOnError, ignoreCase: ignoreCase, defaultAssemblyName: callingAssemblyName);
}

// This entry is used to implement Type.GetType()'s ability to detect the calling assembly and use it as
// a default assembly name.
public static Type ExtensibleGetType(string typeName, string callingAssemblyName, Func<AssemblyName, Assembly?> assemblyResolver, Func<Assembly?, string, bool, Type?>? typeResolver, bool throwOnError, bool ignoreCase)
{
return RuntimeAugments.Callbacks.GetType(typeName, assemblyResolver, typeResolver,
throwOnError: throwOnError, ignoreCase: ignoreCase, extensibleParser: true, callingAssemblyName);
return TypeNameParser.GetType(typeName, assemblyResolver, typeResolver, throwOnError: throwOnError, ignoreCase: ignoreCase, defaultAssemblyName: callingAssemblyName);
}

// This supports Assembly.GetExecutingAssembly() intrinsic expansion in the compiler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ internal ref partial struct TypeNameParser
private Assembly? _topLevelAssembly;
private string? _defaultAssemblyName;

internal static Type? GetType(
string typeName,
bool throwOnError = false,
bool ignoreCase = false,
string? defaultAssemblyName = null)
{
return GetType(typeName, assemblyResolver: null, typeResolver: null,
throwOnError: throwOnError, ignoreCase: ignoreCase, extensibleParser: false, defaultAssemblyName: defaultAssemblyName);
}

internal static Type? GetType(
string typeName,
Func<AssemblyName, Assembly?>? assemblyResolver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ private static Type GetTypeFromEETypePtrSlow(EETypePtr eeType, ref GCHandle hand
public static Type GetType(string typeName, bool throwOnError) => GetType(typeName, throwOnError: throwOnError, ignoreCase: false);
[Intrinsic]
[RequiresUnreferencedCode("The type might be removed")]
public static Type GetType(string typeName, bool throwOnError, bool ignoreCase) => RuntimeAugments.Callbacks.GetType(typeName, null, null, throwOnError: throwOnError, ignoreCase: ignoreCase, extensibleParser: false, defaultAssembly: null);
public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
{
return TypeNameParser.GetType(typeName, throwOnError: throwOnError, ignoreCase: ignoreCase);
}

[Intrinsic]
[RequiresUnreferencedCode("The type might be removed")]
Expand All @@ -81,6 +84,9 @@ private static Type GetTypeFromEETypePtrSlow(EETypePtr eeType, ref GCHandle hand
public static Type GetType(string typeName, Func<AssemblyName, Assembly?>? assemblyResolver, Func<Assembly?, string, bool, Type?>? typeResolver, bool throwOnError) => GetType(typeName, assemblyResolver, typeResolver, throwOnError: throwOnError, ignoreCase: false);
[Intrinsic]
[RequiresUnreferencedCode("The type might be removed")]
public static Type GetType(string typeName, Func<AssemblyName, Assembly?>? assemblyResolver, Func<Assembly?, string, bool, Type?>? typeResolver, bool throwOnError, bool ignoreCase) => RuntimeAugments.Callbacks.GetType(typeName, assemblyResolver, typeResolver, throwOnError: throwOnError, ignoreCase: ignoreCase, extensibleParser: true, defaultAssembly: null);
public static Type GetType(string typeName, Func<AssemblyName, Assembly?>? assemblyResolver, Func<Assembly?, string, bool, Type?>? typeResolver, bool throwOnError, bool ignoreCase)
{
return TypeNameParser.GetType(typeName, assemblyResolver, typeResolver, throwOnError: throwOnError, ignoreCase: ignoreCase);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ internal class ReflectionExecutionDomainCallbacksImplementation : ReflectionExec
public override MethodBase GetMethodBaseFromStartAddressIfAvailable(IntPtr methodStartAddress) => null;
public override Type GetNamedTypeForHandle(RuntimeTypeHandle typeHandle, bool isGenericTypeDefinition) => RuntimeTypeInfo.GetRuntimeTypeInfo(typeHandle);
public override Type GetPointerTypeForHandle(RuntimeTypeHandle typeHandle) => RuntimeTypeInfo.GetRuntimeTypeInfo(typeHandle);
public override Type GetType(string typeName, Func<AssemblyName, Assembly?> assemblyResolver, Func<Assembly?, string, bool, Type?>? typeResolver, bool throwOnError, bool ignoreCase, bool extensibleParser, string defaultAssembly) => throw new NotSupportedException(SR.Reflection_Disabled);
public override RuntimeTypeHandle GetTypeHandleIfAvailable(Type type) => type.TypeHandle;
public override bool IsReflectionBlocked(RuntimeTypeHandle typeHandle) => false;
public override bool SupportsReflection(Type type) => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public ReflectionExecutionDomainCallbacksImplementation(ExecutionDomain executio
_executionEnvironment = executionEnvironment;
}

public sealed override Type GetType(string typeName, Func<AssemblyName, Assembly> assemblyResolver, Func<Assembly, string, bool, Type> typeResolver, bool throwOnError, bool ignoreCase, bool extensibleParser, string defaultAssemblyName)
{
return _executionDomain.GetType(typeName, assemblyResolver, typeResolver, throwOnError, ignoreCase, extensibleParser, defaultAssemblyName);
}

public sealed override bool IsReflectionBlocked(RuntimeTypeHandle typeHandle)
{
return _executionEnvironment.IsReflectionBlocked(typeHandle);
Expand Down

0 comments on commit 9f89797

Please sign in to comment.