Ejemplo n.º 1
0
        /// <summary>
        /// 从枚举类型中后去物料属性值。
        /// </summary>
        /// <param name="attribute">物料属性</param>
        /// <param name="enumType">枚举类型</param>
        /// <param name="listAttribute">标注属性值类型为列表时使用的Attribute</param>
        /// <returns></returns>
        private static IList <TAttributeValue> GetAttributeValuesFromEnum(TAttribute attribute, Type enumType, AttributeValueListAttribute listAttribute)
        {
            List <TAttributeValue> aValues = new List <TAttributeValue>();
            Array arr   = Enum.GetValues(enumType);
            Regex regex = null;

            if (listAttribute != null && !String.IsNullOrEmpty(listAttribute.NamePattern))
            {
                regex = new Regex(listAttribute.NamePattern);
            }
            for (int i = 0; i < arr.Length; i++)
            {
                object val = arr.GetValue(i);
                val = Convert.ToInt64(val);
                string name = Enum.GetName(enumType, val);
                if (regex != null && !regex.IsMatch(name))
                {
                    continue;
                }
                TAttributeValue aValue = (TAttributeValue)attribute.NewCreateAttributeValue();// new TAttributeValue();
                aValue.AttributeValueName = name;
                aValue.Value       = Convert.ToString(val);
                aValue.Active      = true;
                aValue.Description = name;
                aValue.AttributeId = attribute.AttributeId;
                aValues.Add(aValue);
                attribute.AttributeValues.Add(aValue);
            }
            return(aValues);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从列表类型的静态字段(类成员,非实例成员)中获取物料属性值。
        /// </summary>
        /// <param name="attribute">物料属性</param>
        /// <param name="listType">列表类型</param>
        /// <param name="listAttribute">标注属性值类型为列表时使用的Attribute</param>
        /// <returns></returns>
        private static IList <TAttributeValue> GetAttributeValuesFromStaticFields(TAttribute attribute, Type listType, AttributeValueListAttribute listAttribute)
        {
            FieldInfo[] fieldInfos = listType.GetFields(BindingFlags.Public | BindingFlags.Static);
            if (fieldInfos == null || fieldInfos.Length == 0)
            {
                return(null);
            }
            List <TAttributeValue> aValues = new List <TAttributeValue>();
            Regex regex = null;

            if (!String.IsNullOrEmpty(listAttribute.NamePattern))
            {
                regex = new Regex(listAttribute.NamePattern);
            }
            foreach (FieldInfo p in fieldInfos)
            {
                if (regex != null && !regex.IsMatch(p.Name))
                {
                    continue;
                }
                object          val    = p.GetValue(null);
                string          name   = p.Name;
                TAttributeValue aValue = (TAttributeValue)attribute.NewCreateAttributeValue();//new TAttributeValue();
                aValue.AttributeValueName = name;
                aValue.Value       = Convert.ToString(val);
                aValue.Active      = true;
                aValue.Description = name;
                aValue.AttributeId = attribute.AttributeId;
                aValues.Add(aValue);
                attribute.AttributeValues.Add(aValue);
            }
            return(aValues);
        }