public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Create a new DecimalConstantAttribute instance."); try { DecimalConstantAttribute myDAttribute = new DecimalConstantAttribute(0, 0, 0x00000000, 0x00000000, 0x00000000); if (myDAttribute == null) { TestLibrary.TestFramework.LogError("001.1", "Occurs error when Construct DecimalConstantAttribute !"); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 1, 0x00000000, 0x00000000, 0x00000000); if (myDAttribute == null) { TestLibrary.TestFramework.LogError("001.2", "Occurs error when Construct DecimalConstantAttribute !"); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 0, 0x00000000, 0x00000000, 0x00000001); if (myDAttribute == null) { TestLibrary.TestFramework.LogError("001.3", "Occurs error when Construct DecimalConstantAttribute !"); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 1, 0x00000000, 0x00000000, 0x00000001); if (myDAttribute == null) { TestLibrary.TestFramework.LogError("001.4", "Occurs error when Construct DecimalConstantAttribute !"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.0", "Unexpected exception occurs: " + e); retVal = false; } return retVal; }
private Object GetDefaultValueInternal(bool raw) { Contract.Assert(!m_noMetadata); if (m_noDefaultValue) { return(DBNull.Value); } object defaultValue = null; // Why check the parameter type only for DateTime and only for the ctor arguments? // No check on the parameter type is done for named args and for Decimal. // We should move this after MdToken.IsNullToken(m_tkParamDef) and combine it // with the other custom attribute logic. But will that be a breaking change? // For a DateTime parameter on which both an md constant and a ca constant are set, // which one should win? if (ParameterType == typeof(DateTime)) { if (raw) { CustomAttributeTypedArgument value = CustomAttributeData.Filter( CustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0); if (value.ArgumentType != null) { return(new DateTime((long)value.Value)); } } else { object[] dt = GetCustomAttributes(typeof(DateTimeConstantAttribute), false); if (dt != null && dt.Length != 0) { return(((DateTimeConstantAttribute)dt[0]).Value); } } } #region Look for a default value in metadata if (!MdToken.IsNullToken(m_tkParamDef)) { // This will return DBNull.Value if no constant value is defined on m_tkParamDef in the metadata. defaultValue = MdConstant.GetValue(m_scope, m_tkParamDef, ParameterType.GetTypeHandleInternal(), raw); } #endregion if (defaultValue == DBNull.Value) { #region Look for a default value in the custom attributes if (raw) { foreach (CustomAttributeData attr in CustomAttributeData.GetCustomAttributes(this)) { Type attrType = attr.Constructor.DeclaringType; if (attrType == typeof(DateTimeConstantAttribute)) { defaultValue = DateTimeConstantAttribute.GetRawDateTimeConstant(attr); } else if (attrType == typeof(DecimalConstantAttribute)) { defaultValue = DecimalConstantAttribute.GetRawDecimalConstant(attr); } else if (attrType.IsSubclassOf(s_CustomConstantAttributeType)) { defaultValue = CustomConstantAttribute.GetRawConstant(attr); } } } else { Object[] CustomAttrs = GetCustomAttributes(s_CustomConstantAttributeType, false); if (CustomAttrs.Length != 0) { defaultValue = ((CustomConstantAttribute)CustomAttrs[0]).Value; } else { CustomAttrs = GetCustomAttributes(s_DecimalConstantAttributeType, false); if (CustomAttrs.Length != 0) { defaultValue = ((DecimalConstantAttribute)CustomAttrs[0]).Value; } } } #endregion } if (defaultValue == DBNull.Value) { m_noDefaultValue = true; } return(defaultValue); }
public static bool TryFindRawDefaultValueFromCustomAttributes(this CustomAttributeHandleCollection handles, EcmaModule module, out object rawDefaultValue) { rawDefaultValue = default; MetadataReader reader = module.Reader; foreach (CustomAttributeHandle handle in handles) { CustomAttribute ca = handle.GetCustomAttribute(reader); EntityHandle declaringTypeHandle = ca.TryGetDeclaringTypeHandle(reader); if (declaringTypeHandle.IsNil) { continue; } if (declaringTypeHandle.TypeMatchesNameAndNamespace(Utf8Constants.SystemRuntimeCompilerServices, Utf8Constants.DateTimeConstantAttribute, reader)) { CustomAttributeData cad = handle.ToCustomAttributeData(module); IList <CustomAttributeTypedArgument> cats = cad.ConstructorArguments; if (cats.Count != 1) { return(false); } CoreTypes ct = module.Loader.GetAllFoundCoreTypes(); if (cats[0].ArgumentType != ct[CoreType.Int64]) { return(false); } long ticks = (long)(cats[0].Value); rawDefaultValue = new DateTimeConstantAttribute(ticks).Value; return(true); } if (declaringTypeHandle.TypeMatchesNameAndNamespace(Utf8Constants.SystemRuntimeCompilerServices, Utf8Constants.DecimalConstantAttribute, reader)) { CustomAttributeData cad = handle.ToCustomAttributeData(module); IList <CustomAttributeTypedArgument> cats = cad.ConstructorArguments; if (cats.Count != 5) { return(false); } CoreTypes ct = module.Loader.GetAllFoundCoreTypes(); if (cats[0].ArgumentType != ct[CoreType.Byte] || cats[1].ArgumentType != ct[CoreType.Byte]) { return(false); } byte scale = (byte)cats[0].Value; byte sign = (byte)cats[1].Value; if (cats[2].ArgumentType == ct[CoreType.Int32] && cats[3].ArgumentType == ct[CoreType.Int32] && cats[4].ArgumentType == ct[CoreType.Int32]) { int hi = (int)cats[2].Value; int mid = (int)cats[3].Value; int lo = (int)cats[4].Value; rawDefaultValue = new DecimalConstantAttribute(scale, sign, hi, mid, lo).Value; return(true); } if (cats[2].ArgumentType == ct[CoreType.UInt32] && cats[3].ArgumentType == ct[CoreType.UInt32] && cats[4].ArgumentType == ct[CoreType.UInt32]) { uint hi = (uint)cats[2].Value; uint mid = (uint)cats[3].Value; uint lo = (uint)cats[4].Value; rawDefaultValue = new DecimalConstantAttribute(scale, sign, hi, mid, lo).Value; return(true); } return(false); } // Should we also look for CustomConstantAttribute too? Who uses that (other than DateTimeConstantAttribute which // we handled above?) CustomConstantAttribute is an abstract class which means we have to figure out how a subclass // we've never heard of would set the "Value" property which is kinda hard to do when you can't Invoke(). // Even the CLR doesn't return consistent values for this between the raw and non-raw versions. // Even doing the subclass check would open the door to resolving types and dependency assemblies and their // resulting FileNotFoundExceptions. Which is exactly what we're trying to avoid with this name-based lookup approach. } return(false); }
public bool NegTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest1: scale > 28."); try { DecimalConstantAttribute myDAttribute = new DecimalConstantAttribute(29, 0, 0x00000000, 0x00000000, 0x00000000); TestLibrary.TestFramework.LogError("101.1", "ArgumentOutOfRangeException should be caught."); retVal = false; } catch (ArgumentOutOfRangeException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("101.0", "Unexpected exception occurs: " + e); retVal = false; } return retVal; }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Get the DecimalConstantAttribute Value."); try { DecimalConstantAttribute myDAttribute = new DecimalConstantAttribute(0, 0, 0x00000000, 0x00000000, 0x00000000); decimal expectDecimal = 0; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.1", "Occurs error when Construct DecimalConstantAttribute !"); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 1, 0x00000000, 0x00000000, 0x00000000); expectDecimal = 0; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.2", "Occurs error when Construct DecimalConstantAttribute,should return " + expectDecimal); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 0, 0x00000000, 0x00000000, 0x00000001); expectDecimal = 1e-28m; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.3", "Occurs error when Construct DecimalConstantAttribute ,should return " + expectDecimal); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 1, 0x00000000, 0x00000000, 0x00000001); expectDecimal = -(1e-28m); if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.4", "Occurs error when Construct DecimalConstantAttribute ,should return " + expectDecimal); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 1, 0x00000001, 0x00000001, 0x00000001); expectDecimal = -0.0000000018446744078004518913m; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.5", "Occurs error when Construct DecimalConstantAttribute ,should return " + expectDecimal); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 100, 0x00000001, 0x00000001, 0x00000001); expectDecimal = -0.0000000018446744078004518913m; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.6", "Occurs error when Construct DecimalConstantAttribute ,should return " + expectDecimal); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.0", "Unexpected exception occurs: " + e); retVal = false; } return(retVal); }
// Test the DecimalConstantAttribute class. public void TestDecimalConstantAttribute() { DecimalConstantAttribute attr; Decimal value; int[] bits; value = 123.45m; bits = Decimal.GetBits(value); attr = new DecimalConstantAttribute ((byte)(bits[3] >> 16), (byte)((bits[3] & unchecked((int)0x80000000)) != 0 ? 1 : 0), unchecked((uint)(bits[2])), unchecked((uint)(bits[1])), unchecked((uint)(bits[0]))); AssertEquals("DC (1)", value, attr.Value); value = -7655578123.45m; bits = Decimal.GetBits(value); attr = new DecimalConstantAttribute ((byte)(bits[3] >> 16), (byte)((bits[3] & unchecked((int)0x80000000)) != 0 ? 1 : 0), unchecked((uint)(bits[2])), unchecked((uint)(bits[1])), unchecked((uint)(bits[0]))); AssertEquals("DC (2)", value, attr.Value); }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Get the DecimalConstantAttribute Value."); try { DecimalConstantAttribute myDAttribute = new DecimalConstantAttribute(0, 0, 0x00000000, 0x00000000, 0x00000000); decimal expectDecimal = 0; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.1", "Occurs error when Construct DecimalConstantAttribute !"); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 1, 0x00000000, 0x00000000, 0x00000000); expectDecimal = 0; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.2", "Occurs error when Construct DecimalConstantAttribute,should return " + expectDecimal); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 0, 0x00000000, 0x00000000, 0x00000001); expectDecimal = 1e-28m; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.3", "Occurs error when Construct DecimalConstantAttribute ,should return " + expectDecimal); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 1, 0x00000000, 0x00000000, 0x00000001); expectDecimal = -(1e-28m); if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.4", "Occurs error when Construct DecimalConstantAttribute ,should return " + expectDecimal); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 1, 0x00000001, 0x00000001, 0x00000001); expectDecimal = -0.0000000018446744078004518913m; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.5", "Occurs error when Construct DecimalConstantAttribute ,should return " + expectDecimal); retVal = false; } myDAttribute = new DecimalConstantAttribute(28, 100, 0x00000001, 0x00000001, 0x00000001); expectDecimal = -0.0000000018446744078004518913m; if (myDAttribute.Value != expectDecimal) { TestLibrary.TestFramework.LogError("001.6", "Occurs error when Construct DecimalConstantAttribute ,should return " + expectDecimal); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.0", "Unexpected exception occurs: " + e); retVal = false; } return retVal; }
private object GetDefaultValueInternal(bool raw) { if (this.m_noDefaultValue) { return(DBNull.Value); } object obj = null; if (this.ParameterType == typeof(DateTime)) { if (raw) { CustomAttributeTypedArgument customAttributeTypedArgument = CustomAttributeData.Filter(CustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0); if (customAttributeTypedArgument.ArgumentType != null) { return(new DateTime((long)customAttributeTypedArgument.Value)); } } else { object[] customAttributes = this.GetCustomAttributes(typeof(DateTimeConstantAttribute), false); if (customAttributes != null && customAttributes.Length != 0) { return(((DateTimeConstantAttribute)customAttributes[0]).Value); } } } if (!System.Reflection.MetadataToken.IsNullToken(this.m_tkParamDef)) { obj = MdConstant.GetValue(this.m_scope, this.m_tkParamDef, this.ParameterType.GetTypeHandleInternal(), raw); } if (obj == DBNull.Value) { if (raw) { using (IEnumerator <CustomAttributeData> enumerator = CustomAttributeData.GetCustomAttributes(this).GetEnumerator()) { while (enumerator.MoveNext()) { CustomAttributeData customAttributeData = enumerator.Current; Type declaringType = customAttributeData.Constructor.DeclaringType; if (declaringType == typeof(DateTimeConstantAttribute)) { obj = DateTimeConstantAttribute.GetRawDateTimeConstant(customAttributeData); } else if (declaringType == typeof(DecimalConstantAttribute)) { obj = DecimalConstantAttribute.GetRawDecimalConstant(customAttributeData); } else if (declaringType.IsSubclassOf(RuntimeParameterInfo.s_CustomConstantAttributeType)) { obj = CustomConstantAttribute.GetRawConstant(customAttributeData); } } goto IL_1A7; } } object[] customAttributes2 = this.GetCustomAttributes(RuntimeParameterInfo.s_CustomConstantAttributeType, false); if (customAttributes2.Length != 0) { obj = ((CustomConstantAttribute)customAttributes2[0]).Value; } else { customAttributes2 = this.GetCustomAttributes(RuntimeParameterInfo.s_DecimalConstantAttributeType, false); if (customAttributes2.Length != 0) { obj = ((DecimalConstantAttribute)customAttributes2[0]).Value; } } } IL_1A7: if (obj == DBNull.Value) { this.m_noDefaultValue = true; } return(obj); }
private object GetDefaultValueInternal(bool raw) { if (this.m_noDefaultValue) { return((object)DBNull.Value); } object obj = (object)null; if (this.ParameterType == typeof(DateTime)) { if (raw) { CustomAttributeTypedArgument attributeTypedArgument = CustomAttributeData.Filter(CustomAttributeData.GetCustomAttributes((ParameterInfo)this), typeof(DateTimeConstantAttribute), 0); if (attributeTypedArgument.ArgumentType != (Type)null) { return((object)new DateTime((long)attributeTypedArgument.Value)); } } else { object[] customAttributes = this.GetCustomAttributes(typeof(DateTimeConstantAttribute), false); if (customAttributes != null && customAttributes.Length != 0) { return(((CustomConstantAttribute)customAttributes[0]).Value); } } } if (!System.Reflection.MetadataToken.IsNullToken(this.m_tkParamDef)) { obj = MdConstant.GetValue(this.m_scope, this.m_tkParamDef, this.ParameterType.GetTypeHandleInternal(), raw); } if (obj == DBNull.Value) { if (raw) { foreach (CustomAttributeData customAttribute in (IEnumerable <CustomAttributeData>)CustomAttributeData.GetCustomAttributes((ParameterInfo)this)) { Type declaringType = customAttribute.Constructor.DeclaringType; if (declaringType == typeof(DateTimeConstantAttribute)) { obj = (object)DateTimeConstantAttribute.GetRawDateTimeConstant(customAttribute); } else if (declaringType == typeof(DecimalConstantAttribute)) { obj = (object)DecimalConstantAttribute.GetRawDecimalConstant(customAttribute); } else if (declaringType.IsSubclassOf(RuntimeParameterInfo.s_CustomConstantAttributeType)) { obj = CustomConstantAttribute.GetRawConstant(customAttribute); } } } else { object[] customAttributes1 = this.GetCustomAttributes(RuntimeParameterInfo.s_CustomConstantAttributeType, false); if (customAttributes1.Length != 0) { obj = ((CustomConstantAttribute)customAttributes1[0]).Value; } else { object[] customAttributes2 = this.GetCustomAttributes(RuntimeParameterInfo.s_DecimalConstantAttributeType, false); if (customAttributes2.Length != 0) { obj = (object)((DecimalConstantAttribute)customAttributes2[0]).Value; } } } } if (obj == DBNull.Value) { this.m_noDefaultValue = true; } return(obj); }