Пример #1
0
        IAttribute <T> CreateAttribute <T>(string name)
        {
            Type attribType = typeof(T);

            if (AllowedAttributes != null && AllowedAttributes.ContainsKey(name))
            {
                return((IAttribute <T>)AllowedAttributes[name]());
            }
            //if (!dynamicAttributesPermitted)
            //    throw new ArgumentOutOfRangeException("name", "Attribute {0} is not allowed on this element.".With(name));

            if (attribType.IsValueType ||
                attribType == typeof(string) ||
                typeof(Nullable).IsAssignableFrom(attribType))
            {
                return(new PrimaryTypeAttributeNode <T>(name));
            }
            if (attribType == typeof(MediaType))
            {
                return((IAttribute <T>) new XhtmlAttributeNode <MediaType>(name, false, media => media.ToString(), str => new MediaType(str)));
            }
            if (attribType == typeof(IList <Uri>))
            {
                return((IAttribute <T>) new CharacterSeparatedAttributeNode <Uri>(name, " ", uri => uri.ToString(), s => new Uri(s, UriKind.Absolute)));
            }
            if (attribType == typeof(IList <MediaType>))
            {
                return((IAttribute <T>) new CharacterSeparatedAttributeNode <MediaType>(name, " ", mediatype => mediatype.ToString(), str => new MediaType(str)));
            }
            if (attribType == typeof(IList <string>))
            {
                return((IAttribute <T>) new CharacterSeparatedAttributeNode <string>(name, " ", i => i, i => i));
            }
            throw new InvalidOperationException("Could not automatically create attribute of type " + typeof(T));
        }
Пример #2
0
 public IAttribute this[string key]
 {
     get
     {
         if (_attributes[key] == null && AllowedAttributes != null)
         {
             if (AllowedAttributes.ContainsKey(key))
             {
                 _attributes[key] = AllowedAttributes[key]();
             }
             else
             {
                 _attributes[key] = new PrimaryTypeAttributeNode <string>(key);
             }
         }
         return(_attributes[key]);
     }
     set { _attributes[key] = value; }
 }