Пример #1
0
        /// <summary>
        /// Extracts the media descriptions via reflection on a <see cref="Type"/>.
        /// </summary>
        /// <returns>
        /// It returns a <see cref="Dictionary{TMediaFormat,MediaFormatAttribute}"/> mapping all known media formats with the relative
        /// <see cref="MediaFormatAttribute"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="type"/> is null.
        /// </exception>
        protected void ExtractDescriptions(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            Dictionary <string, MediaFormatAttribute> formatDescriptions = new Dictionary <string, MediaFormatAttribute>();

            FieldInfo[] mediaFormatMembers = type.GetFields(BindingFlags.Public | BindingFlags.Static);

            foreach (FieldInfo mediaFormatField in mediaFormatMembers)
            {
                MediaFormatAttribute mediaFormatAttribute = (MediaFormatAttribute)Attribute.GetCustomAttribute(mediaFormatField, typeof(MediaFormatAttribute));

                if ((mediaFormatAttribute != null) && (mediaFormatAttribute.Abstract == false))
                {
                    formatDescriptions[(string)mediaFormatField.GetValue(null)] = mediaFormatAttribute;
                }
            }

            foreach (KeyValuePair <string, MediaFormatAttribute> pair in formatDescriptions)
            {
                if (mCodecDescriptions.ContainsKey(pair.Key))
                {
                    Resource.Log("The type {0} define media {1}, but it is ignored because it is already defined elsewhere.", type, pair.Key);
                    continue;
                }

                mCodecDescriptions.Add(pair.Key, pair.Value);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the possible media formats from URL.
        /// </summary>
        /// <param name="url">
        /// A <see cref="String"/> that specify the URL where media is located.
        /// </param>
        /// <returns>
        /// A <see cref="IEnumerable{TMediaFormat}"/> enumerating all possible media formats given <paramref name="url"/>. This
        /// method could return an empty enumeration.
        /// </returns>
        /// <exception cref='ArgumentNullException'>
        /// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
        /// </exception>
        public IEnumerable <string> GetFormatsFromUrl(string url)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            List <string> mediaFormats = new List <string>();

            foreach (KeyValuePair <string, MediaFormatAttribute> pair in mCodecDescriptions)
            {
                MediaFormatAttribute mediaFormat = pair.Value;

                if (mediaFormat.MatchPattern(url))
                {
                    mediaFormats.Add(pair.Key);
                }
            }

            return(mediaFormats);
        }