Esempio n. 1
0
        public static bool GetCustomAttributeDefaultValueIfAny(IEnumerable <CustomAttributeData> customAttributes, bool raw, out object defaultValue)
        {
            // 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)))
                {
                    if (raw)
                    {
                        foreach (CustomAttributeNamedArgument namedArgument in cad.NamedArguments)
                        {
                            if (namedArgument.MemberInfo.Name.Equals("Value"))
                            {
                                defaultValue = namedArgument.TypedValue.Value;
                                return(true);
                            }
                        }
                        defaultValue = null;
                        return(false);
                    }
                    else
                    {
                        CustomConstantAttribute customConstantAttribute = (CustomConstantAttribute)(cad.Instantiate());
                        defaultValue = customConstantAttribute.Value;
                        return(true);
                    }
                }
                if (attributeType.Equals(typeof(DecimalConstantAttribute)))
                {
                    // We should really do a non-instanting check if "raw == false" but given that we don't support
                    // reflection-only loads, there isn't an observable difference.
                    DecimalConstantAttribute decimalConstantAttribute = (DecimalConstantAttribute)(cad.Instantiate());
                    defaultValue = decimalConstantAttribute.Value;
                    return(true);
                }
            }

            defaultValue = null;
            return(false);
        }
        private static bool GetCustomAttributeDefaultValueIfAny(IEnumerable <CustomAttributeData> customAttributes, out object defaultValue)
        {
            // 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);
        }
Esempio n. 3
0
        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.GetTypeInfo().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;
                    TypeInfo attributeTypeInfo = attributeType.GetTypeInfo();
                    if (attributeTypeInfo.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);
        }
Esempio 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);
        }
        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);
        }
Esempio n. 6
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);
        }