Пример #1
0
        public object GetConstantValue(bool throwOnInvalidMetadata)
        {
            try {
                var metadata     = module.metadata;
                var parameterDef = metadata.GetParameter(handle);
                if (IsDecimalConstant)
                {
                    return(DecimalConstantHelper.GetDecimalConstantValue(module, parameterDef.GetCustomAttributes()));
                }

                var constantHandle = parameterDef.GetDefaultValue();
                if (constantHandle.IsNil)
                {
                    return(null);
                }

                var constant   = metadata.GetConstant(constantHandle);
                var blobReader = metadata.GetBlobReader(constant.Value);
                try {
                    return(blobReader.ReadConstant(constant.TypeCode));
                } catch (ArgumentOutOfRangeException) {
                    throw new BadImageFormatException($"Constant with invalid typecode: {constant.TypeCode}");
                }
            } catch (BadImageFormatException) when(!throwOnInvalidMetadata)
            {
                return(null);
            }
        }
Пример #2
0
        public object GetConstantValue(bool throwOnInvalidMetadata)
        {
            object val = LazyInit.VolatileRead(ref this.constantValue);

            if (val != null)
            {
                return(val);
            }
            try {
                var metadata = module.metadata;
                var fieldDef = metadata.GetFieldDefinition(handle);
                if (IsDecimalConstant && DecimalConstantHelper.AllowsDecimalConstants(module))
                {
                    val = DecimalConstantHelper.GetDecimalConstantValue(module, fieldDef.GetCustomAttributes());
                }
                else
                {
                    var constantHandle = fieldDef.GetDefaultValue();
                    if (constantHandle.IsNil)
                    {
                        return(null);
                    }
                    var constant   = metadata.GetConstant(constantHandle);
                    var blobReader = metadata.GetBlobReader(constant.Value);
                    try {
                        val = blobReader.ReadConstant(constant.TypeCode);
                    } catch (ArgumentOutOfRangeException) {
                        throw new BadImageFormatException($"Constant with invalid typecode: {constant.TypeCode}");
                    }
                }
                return(LazyInit.GetOrSet(ref this.constantValue, val));
            } catch (BadImageFormatException) when(!throwOnInvalidMetadata)
            {
                return(null);
            }
        }