Skip to content

Commit

Permalink
DisableReflection
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas committed Oct 28, 2023
1 parent ba8f71c commit baea1e5
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 603 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ The .NET Foundation licenses this file to you under the MIT license.
<AutoInitializedAssemblies Include="System.Private.StackTraceMetadata" Condition="$(StackTraceSupport) != 'false'" />
<AutoInitializedAssemblies Include="System.Private.TypeLoader" />
<AutoInitializedAssemblies Include="System.Private.Reflection.Execution" Condition="$(IlcDisableReflection) != 'true'" />
<AutoInitializedAssemblies Include="System.Private.DisabledReflection" Condition="$(IlcDisableReflection) == 'true'" />
</ItemGroup>

<ItemDefinitionGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public override Array GetEnumValuesAsUnderlyingType()
throw new ArgumentException(SR.Arg_MustBeEnum, "enumType");
}

public override int GetHashCode()
=> RuntimeHelpers.GetHashCode(this);

public override RuntimeTypeHandle TypeHandle
=> GetRuntimeTypeInfo().TypeHandle;

Expand Down Expand Up @@ -183,6 +186,11 @@ public override Type GetFunctionPointerReturnType()
// Implementation shared with MetadataType
//

public override string ToString()
=> GetRuntimeTypeInfo().ToString();

public override bool Equals(object? obj) => ReferenceEquals(obj, this);

object ICloneable.Clone() => this;

public override bool IsSecurityCritical => true;
Expand Down Expand Up @@ -289,6 +297,8 @@ public override IEnumerable<CustomAttributeData> CustomAttributes
public override IList<CustomAttributeData> GetCustomAttributesData()
=> GetRuntimeTypeInfo().GetCustomAttributesData();

public override string Name => GetRuntimeTypeInfo().Name;

public override string? Namespace => GetRuntimeTypeInfo().Namespace;

public override string? AssemblyQualifiedName => GetRuntimeTypeInfo().AssemblyQualifiedName;
Expand All @@ -301,8 +311,6 @@ public override IList<CustomAttributeData> GetCustomAttributesData()

public override Guid GUID => GetRuntimeTypeInfo().GUID;

public override string Name => GetRuntimeTypeInfo().Name;

public override bool HasSameMetadataDefinitionAs(MemberInfo other) => GetRuntimeTypeInfo().HasSameMetadataDefinitionAs(other);

public override Type MakePointerType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ internal RuntimeType(MethodTable* pUnderlyingEEType)

internal RuntimeTypeInfo GetRuntimeTypeInfo() => _runtimeTypeInfo ?? CreateRuntimeTypeInfo();

private static bool IsReflectionDisabled => false;

private static bool DoNotThrowForNames => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForNames", out bool doNotThrow) && doNotThrow;
private static bool DoNotThrowForAssembly => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForAssembly", out bool doNotThrow) && doNotThrow;
private static bool DoNotThrowForAttributes => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForAttributes", out bool doNotThrow) && doNotThrow;

private RuntimeTypeInfo CreateRuntimeTypeInfo()
{
if (IsReflectionDisabled)
throw new NotSupportedException(SR.Reflection_Disabled);

EETypePtr eeType = ToEETypePtr();

RuntimeTypeHandle runtimeTypeHandle = new RuntimeTypeHandle(eeType);
Expand Down Expand Up @@ -186,6 +195,9 @@ public override Array GetEnumValuesAsUnderlyingType()
return Enum.GetValuesAsUnderlyingType(this);
}

public override int GetHashCode()
=> ((nuint)_pUnderlyingEEType).GetHashCode();

public override RuntimeTypeHandle TypeHandle
=> new RuntimeTypeHandle(_pUnderlyingEEType);

Expand Down Expand Up @@ -267,7 +279,7 @@ public override Type[] GetInterfaces()
}

public override bool IsTypeDefinition
=> (_pUnderlyingEEType->IsCanonical && !_pUnderlyingEEType->IsGeneric) || _pUnderlyingEEType->IsGenericTypeDefinition;
=> (_pUnderlyingEEType->IsCanonical && !_pUnderlyingEEType->IsGeneric) || _pUnderlyingEEType->IsGenericTypeDefinition;

public override bool IsGenericType
=> _pUnderlyingEEType->IsGeneric || _pUnderlyingEEType->IsGenericTypeDefinition;
Expand Down Expand Up @@ -375,6 +387,16 @@ public override Type GetFunctionPointerReturnType()
// Implementation shared with MetadataType
//

public override string ToString()
{
if (IsReflectionDisabled)
return "0x" + ((nuint)_pUnderlyingEEType).ToString("x");

return GetRuntimeTypeInfo().ToString();
}

public override bool Equals(object? obj) => ReferenceEquals(obj, this);

object ICloneable.Clone() => this;

public override bool IsSecurityCritical => true;
Expand Down Expand Up @@ -470,31 +492,81 @@ public override bool IsDefined(Type attributeType, bool inherit)
=> GetRuntimeTypeInfo().IsDefined(attributeType, inherit);

public override object[] GetCustomAttributes(bool inherit)
=> GetRuntimeTypeInfo().GetCustomAttributes(inherit);
{
if (IsReflectionDisabled && DoNotThrowForAttributes)
return Array.Empty<object>();

return GetRuntimeTypeInfo().GetCustomAttributes(inherit);
}

public override object[] GetCustomAttributes(Type attributeType, bool inherit)
=> GetRuntimeTypeInfo().GetCustomAttributes(attributeType, inherit);
{
if (IsReflectionDisabled && DoNotThrowForAttributes)
return Array.Empty<object>();

return GetRuntimeTypeInfo().GetCustomAttributes(attributeType, inherit);
}

public override IEnumerable<CustomAttributeData> CustomAttributes
=> GetRuntimeTypeInfo().CustomAttributes;
{
get
{
if (IsReflectionDisabled && DoNotThrowForAttributes)
return Array.Empty<CustomAttributeData>();

return GetRuntimeTypeInfo().CustomAttributes;
}
}

public override IList<CustomAttributeData> GetCustomAttributesData()
=> GetRuntimeTypeInfo().GetCustomAttributesData();
{
if (IsReflectionDisabled && DoNotThrowForAttributes)
return Array.Empty<CustomAttributeData>();

public override string? Namespace => GetRuntimeTypeInfo().Namespace;
return GetRuntimeTypeInfo().GetCustomAttributesData();
}

public override string Name
{
get
{
if (IsReflectionDisabled && DoNotThrowForNames)
return ToString();

return GetRuntimeTypeInfo().Name;
}
}

public override string? Namespace
{
get
{
if (IsReflectionDisabled && DoNotThrowForNames)
return null;

return GetRuntimeTypeInfo().Namespace;
}
}

public override string? AssemblyQualifiedName => GetRuntimeTypeInfo().AssemblyQualifiedName;

public override string? FullName => GetRuntimeTypeInfo().FullName;

public override Assembly Assembly => GetRuntimeTypeInfo().Assembly;
public override Assembly Assembly
{
get
{
if (IsReflectionDisabled && DoNotThrowForAssembly)
return null!; // TODO!!!!!!!!!!

return GetRuntimeTypeInfo().Assembly;
}
}

public override Module Module => GetRuntimeTypeInfo().Module;

public override Guid GUID => GetRuntimeTypeInfo().GUID;

public override string Name => GetRuntimeTypeInfo().Name;

public override bool HasSameMetadataDefinitionAs(MemberInfo other) => GetRuntimeTypeInfo().HasSameMetadataDefinitionAs(other);

public override Type MakePointerType()
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit baea1e5

Please sign in to comment.