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); } }
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); } }