Exemple #1
0
        public static bool TryGet <T>(this IMFAttributes input, Guid key, IFormatProvider provider, out T value)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (input.GetItemType(key, out var type).IsError)
            {
                value = default;
                return(false);
            }

            T item;

            switch (type)
            {
            case _MF_ATTRIBUTE_TYPE.MF_ATTRIBUTE_BLOB:
                if (typeof(T) == typeof(byte[]))
                {
                    value = (T)(object)GetBlob(input, key);
                    return(true);
                }

                var bytes = GetBlob(input, key);
                if (!Conversions.TryChangeType(bytes, provider, out item))
                {
                    value = default;
                    return(false);
                }

                value = item;
                return(true);

            case _MF_ATTRIBUTE_TYPE.MF_ATTRIBUTE_DOUBLE:
                var dbl = GetDouble(input, key);
                if (!Conversions.TryChangeType(dbl, provider, out item))
                {
                    value = default;
                    return(false);
                }

                value = item;
                return(true);

            case _MF_ATTRIBUTE_TYPE.MF_ATTRIBUTE_GUID:
                var guid = GetGuid(input, key);
                if (!Conversions.TryChangeType(guid, provider, out item))
                {
                    value = default;
                    return(false);
                }

                value = item;
                return(true);

            case _MF_ATTRIBUTE_TYPE.MF_ATTRIBUTE_STRING:
                var s = GetString(input, key);
                if (!Conversions.TryChangeType(s, provider, out item))
                {
                    value = default;
                    return(false);
                }

                value = item;
                return(true);

            case _MF_ATTRIBUTE_TYPE.MF_ATTRIBUTE_UINT32:
                var ui = GetUInt32(input, key);
                if (!Conversions.TryChangeType(ui, provider, out item))
                {
                    value = default;
                    return(false);
                }

                value = item;
                return(true);

            case _MF_ATTRIBUTE_TYPE.MF_ATTRIBUTE_UINT64:
                var ul = GetUInt64(input, key);
                if (!Conversions.TryChangeType(ul, provider, out item))
                {
                    value = default;
                    return(false);
                }

                value = item;
                return(true);

            case _MF_ATTRIBUTE_TYPE.MF_ATTRIBUTE_IUNKNOWN:
                var comType = ComObject.GetGenericComObjectComType(typeof(T));
                if (comType == null)
                {
                    value = (T)GetUnknown(input, key, typeof(T).GUID);
                    return(value != null);
                }

                var unk = GetUnknown(input, key, comType.GUID);
                if (unk == null)
                {
                    value = default;
                    return(false);
                }

                value = (T)(object)ComObject.WrapAsGeneric(comType, unk);
                return(true);

            default:
                throw new NotSupportedException();
            }
        }