Exemplo n.º 1
0
        public static void DateTimeConstantAttributeTests()
        {
            long ticks = DateTime.Now.Ticks;
            var  attr  = new DateTimeConstantAttribute(ticks);

            Assert.Equal(new DateTime(ticks), attr.Value);

            Assert.Throws <ArgumentOutOfRangeException>(() => new DateTimeConstantAttribute(-1));
        }
	// Test the DateTimeConstantAttribute class.
	public void TestDateTimeConstantAttribute()
			{
				DateTimeConstantAttribute attr;

				attr = new DateTimeConstantAttribute(0);
				AssertNotNull("DTC (1)", attr.Value);
				Assert("DTC (2)", (attr.Value is DateTime));
				AssertEquals("DTC (3)", 0, ((DateTime)(attr.Value)).Ticks);

				DateTime now = DateTime.Now;
				attr = new DateTimeConstantAttribute(now.Ticks);
				AssertNotNull("DTC (4)", attr.Value);
				Assert("DTC (5)", (attr.Value is DateTime));
				AssertEquals("DTC (6)", now.Ticks,
							 ((DateTime)(attr.Value)).Ticks);
			}
Exemplo n.º 3
0
    // Test the DateTimeConstantAttribute class.
    public void TestDateTimeConstantAttribute()
    {
        DateTimeConstantAttribute attr;

        attr = new DateTimeConstantAttribute(0);
        AssertNotNull("DTC (1)", attr.Value);
        Assert("DTC (2)", (attr.Value is DateTime));
        AssertEquals("DTC (3)", 0, ((DateTime)(attr.Value)).Ticks);

        DateTime now = DateTime.Now;

        attr = new DateTimeConstantAttribute(now.Ticks);
        AssertNotNull("DTC (4)", attr.Value);
        Assert("DTC (5)", (attr.Value is DateTime));
        AssertEquals("DTC (6)", now.Ticks,
                     ((DateTime)(attr.Value)).Ticks);
    }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
        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);
        }
Exemplo n.º 7
0
        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);
        }