diff options
author | Atsushi Kanamori <AtsushiKan@users.noreply.github.com> | 2017-06-07 21:55:02 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-07 21:55:02 +0300 |
commit | 721e76d5beb52d0d5ed854d8a7d39e5846da12ed (patch) | |
tree | 1c826c258179f49528b8cb0e3f295d57a7b6cb56 /src/System.Private.Reflection.Execution | |
parent | dc772d592486e993d5faec1eab8b4e273c009387 (diff) |
Fix https://github.com/dotnet/corert/issues/3800 (#3823)
This implements the "raw" default value apis properly
so that they bypass enum instantation rather than
unwrapping after the fact (which doesn't go back in
time and undo the exception that gets thrown
if you use GetRawConstantValue() to inspect
an open enum.) Also, "raw" is supposed to change
the way we inspect the custom attributes though
we can get away with some simplification here
given our non-support of reflection-only loads.
This exercise also revealed that the Ecma/Native
refactoring for default values was less than
ideal, so this fixes all that up. It also moves
the native half of default value parsing out
of Reflection.Execution and into Reflection.Core
(it was originally there to honor Reflection.Core
coding rules that are no longer applicable.)
Finally, fixed up the "raw" handling of the
CustomConstantAttribute class to match CoreCLR's.
It does create an observable difference even though
we don't support reflection-only loads...
Diffstat (limited to 'src/System.Private.Reflection.Execution')
3 files changed, 0 insertions, 139 deletions
diff --git a/src/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/DefaultValueParser.cs b/src/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/DefaultValueParser.cs deleted file mode 100644 index 1720d2a35..000000000 --- a/src/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/DefaultValueParser.cs +++ /dev/null @@ -1,117 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using global::System; -using global::System.Reflection; -using global::System.Collections.Generic; -using global::System.Runtime.InteropServices; -using global::System.Runtime.CompilerServices; - -using global::Internal.Runtime.Augments; - -using global::Internal.Reflection.Core; -using global::Internal.Reflection.Core.Execution; -using global::Internal.Reflection.Extensions.NonPortable; - -using global::Internal.Metadata.NativeFormat; - -namespace Internal.Reflection.Execution -{ - internal static class DefaultValueParser - { - public static bool GetDefaultValueIfAny(MemberType memberType, MetadataReader reader, Handle constantHandle, Type declaredType, IEnumerable<CustomAttributeData> customAttributes, out Object defaultValue) - { - if (!(constantHandle.IsNull(reader))) - { - defaultValue = ParseMetadataConstant(reader, constantHandle); - if (declaredType.IsEnum) - defaultValue = Enum.ToObject(declaredType, defaultValue); - return true; - } - - if (memberType != MemberType.Property) // the attributes in question cannot be applied to properties. - { - // Legacy: If there are multiple default value attribute, the desktop picks one at random (and so do we...) - foreach (CustomAttributeData cad in customAttributes) - { - Type attributeType = cad.AttributeType; - if (attributeType.IsSubclassOf(typeof(CustomConstantAttribute))) - { - CustomConstantAttribute customConstantAttribute = (CustomConstantAttribute)(cad.Instantiate()); - defaultValue = customConstantAttribute.Value; - return true; - } - if (attributeType.Equals(typeof(DecimalConstantAttribute))) - { - DecimalConstantAttribute decimalConstantAttribute = (DecimalConstantAttribute)(cad.Instantiate()); - defaultValue = decimalConstantAttribute.Value; - return true; - } - } - } - - defaultValue = null; - return false; - } - - // Not all default value types are permitted in all scenarios. - public enum MemberType - { - Field = 1, - Property = 2, - Parameter = 3, - } - - private static Object ParseMetadataConstant(MetadataReader reader, Handle handle) - { - switch (handle.HandleType) - { - case HandleType.ConstantBooleanValue: - return handle.ToConstantBooleanValueHandle(reader).GetConstantBooleanValue(reader).Value; - - case HandleType.ConstantStringValue: - return handle.ToConstantStringValueHandle(reader).GetConstantStringValue(reader).Value; - - case HandleType.ConstantCharValue: - return handle.ToConstantCharValueHandle(reader).GetConstantCharValue(reader).Value; - - case HandleType.ConstantByteValue: - return handle.ToConstantByteValueHandle(reader).GetConstantByteValue(reader).Value; - - case HandleType.ConstantSByteValue: - return handle.ToConstantSByteValueHandle(reader).GetConstantSByteValue(reader).Value; - - case HandleType.ConstantInt16Value: - return handle.ToConstantInt16ValueHandle(reader).GetConstantInt16Value(reader).Value; - - case HandleType.ConstantUInt16Value: - return handle.ToConstantUInt16ValueHandle(reader).GetConstantUInt16Value(reader).Value; - - case HandleType.ConstantInt32Value: - return handle.ToConstantInt32ValueHandle(reader).GetConstantInt32Value(reader).Value; - - case HandleType.ConstantUInt32Value: - return handle.ToConstantUInt32ValueHandle(reader).GetConstantUInt32Value(reader).Value; - - case HandleType.ConstantInt64Value: - return handle.ToConstantInt64ValueHandle(reader).GetConstantInt64Value(reader).Value; - - case HandleType.ConstantUInt64Value: - return handle.ToConstantUInt64ValueHandle(reader).GetConstantUInt64Value(reader).Value; - - case HandleType.ConstantSingleValue: - return handle.ToConstantSingleValueHandle(reader).GetConstantSingleValue(reader).Value; - - case HandleType.ConstantDoubleValue: - return handle.ToConstantDoubleValueHandle(reader).GetConstantDoubleValue(reader).Value; - - case HandleType.ConstantReferenceValue: - return null; - - default: - throw new BadImageFormatException(); - } - } - } -} diff --git a/src/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.Runtime.cs b/src/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.Runtime.cs index 8aeb9f6fe..bcc6d19ca 100644 --- a/src/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.Runtime.cs +++ b/src/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.Runtime.cs @@ -72,27 +72,6 @@ namespace Internal.Reflection.Execution } //============================================================================================== - // Default Value support. - //============================================================================================== - public sealed override bool GetDefaultValueIfAny(MetadataReader reader, ParameterHandle parameterHandle, Type declaredType, IEnumerable<CustomAttributeData> customAttributes, out Object defaultValue) - { - Parameter parameter = parameterHandle.GetParameter(reader); - return DefaultValueParser.GetDefaultValueIfAny(DefaultValueParser.MemberType.Parameter, reader, parameter.DefaultValue, declaredType, customAttributes, out defaultValue); - } - - public sealed override bool GetDefaultValueIfAny(MetadataReader reader, FieldHandle fieldHandle, Type declaredType, IEnumerable<CustomAttributeData> customAttributes, out Object defaultValue) - { - Field field = fieldHandle.GetField(reader); - return DefaultValueParser.GetDefaultValueIfAny(DefaultValueParser.MemberType.Field, reader, field.DefaultValue, declaredType, customAttributes, out defaultValue); - } - - public sealed override bool GetDefaultValueIfAny(MetadataReader reader, PropertyHandle propertyHandle, Type declaredType, IEnumerable<CustomAttributeData> customAttributes, out Object defaultValue) - { - Property property = propertyHandle.GetProperty(reader); - return DefaultValueParser.GetDefaultValueIfAny(DefaultValueParser.MemberType.Property, reader, property.DefaultValue, declaredType, customAttributes, out defaultValue); - } - - //============================================================================================== // Pseudo Custom Attributes //============================================================================================== public sealed override IEnumerable<CustomAttributeData> GetPseudoCustomAttributes(MetadataReader reader, ScopeDefinitionHandle scopeDefinitionHandle) diff --git a/src/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj b/src/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj index df12d0431..9771e63a0 100644 --- a/src/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj +++ b/src/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj @@ -75,7 +75,6 @@ <Compile Include="Internal\Reflection\Execution\ExecutionEnvironmentImplementation.ManifestResources.cs" /> <Compile Include="Internal\Reflection\Execution\ReflectionExecutionDomainCallbacksImplementation.cs" /> <Compile Include="Internal\Reflection\Execution\MetadataReaderExtensions.cs" /> - <Compile Include="Internal\Reflection\Execution\DefaultValueParser.cs" /> <Compile Include="Internal\Reflection\Execution\MethodInvokeInfo.cs" /> <Compile Include="Internal\Reflection\Execution\RuntimeHandlesExtensions.cs" /> <Compile Include="Internal\Reflection\Execution\FieldAccessors\InstanceFieldAccessor.cs" /> |