Пример #1
0
        public void SetAttributeEnum <T>(XName name, object value)
        {
            object obj = Enum.ToObject(typeof(T), value);

            IDictionary <string, MemberInfo> members =
                obj.GetType().GetTypeInfo().DeclaredMembers.ToDictionary(c => c.Name);

            MemberInfo member;

            if (!members.TryGetValue(obj.ToString(), out member))
            {
                throw new InvalidOperationException();
            }

            IDictionary <Type, CustomAttributeData> customAttributes = member.CustomAttributes.ToDictionary(c => c.AttributeType);

            CustomAttributeData enumMemberAttribute;

            if (customAttributes.TryGetValue(typeof(EnumMemberAttribute), out enumMemberAttribute))
            {
                IDictionary <string, CustomAttributeNamedArgument> args =
                    enumMemberAttribute.NamedArguments.ToDictionary(c => c.MemberName);

                CustomAttributeNamedArgument arg;
                if (args.TryGetValue("Value", out arg))
                {
                    InnerElement.SetAttributeValue(name, arg.TypedValue.Value);
                    return;
                }
            }

            InnerElement.SetAttributeValue(name, ((T)value).ToString());
        }
Пример #2
0
        /// <summary>
        /// Changing curent XElement's attribute value
        /// </summary>
        public void SetAttributeValue(XName name, object value)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            InnerElement.SetAttributeValue(name, value);
        }