/// <summary>
        /// Sets the given package reference collection <paramref name="property"/> if the <paramref name="action"/> does not throw a <see cref="COMException"/> for 0x80070490.
        /// </summary>
        /// <typeparam name="T">The type of the package reference to adapt.</typeparam>
        /// <typeparam name="R">The adapted type of the package reference.</typeparam>
        /// <param name="property">A reference to the property to set.</param>
        /// <param name="propertyName">The name of the property for diagnostic purposes.</param>
        /// <param name="action">A method that returns the value of the property to set.</param>
        /// <param name="creator">A method that creates the adapted reference type.</param>
        /// <param name="error">Optional error handler that accepts the name of the property.</param>
        /// <returns>A <see cref="ReadOnlyCollection{T}"/> containing the adapted package references. This collection may be empty.</returns>
        /// <exception cref="ArgumentException"><paramref name="propertyName"/> is an empty string.</exception>
        /// <exception cref="ArgumentNullException">One or more parameters is null.</exception>
        public static ReadOnlyCollection <R> TrySetCollection <T, R>(
            ref IList <R> property,
            string propertyName,
            Func <IEnumerable <T> > action,
            Func <T, R> creator,
            Action <string> error = null)
            where T : ISetupPackageReference
            where R : PackageReference
        {
            Validate.NotNullOrEmpty(propertyName, nameof(propertyName));
            Validate.NotNull(action, nameof(action));
            Validate.NotNull(creator, nameof(creator));

            var packages = GetAdaptedPackages(action, creator);

            TrySet(ref property, propertyName, packages.ToList, error);

            if (property != null && property.Any())
            {
                return(new ReadOnlyCollection <R>(property));
            }

            return(EmptyReadOnlyCollection <R>());
        }
        /// <summary>
        /// Creates a new <see cref="FailedPackageReference"/> from an <see cref="ISetupFailedPackageReference"/>.
        /// </summary>
        /// <param name="package">The <see cref="ISetupFailedPackageReference"/> to wrap.</param>
        /// <returns>A <see cref="FailedPackageReference"/> that wraps the <see cref="ISetupFailedPackageReference"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="package"/> is null.</exception>
        public static FailedPackageReference Create(ISetupFailedPackageReference package)
        {
            Validate.NotNull(package, nameof(package));

            return(new FailedPackageReference(package));
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Instance"/> class.
        /// </summary>
        /// <param name="instance">The <see cref="ISetupInstance2"/> to adapt.</param>
        /// <exception cref="ArgumentNullException"><paramref name="instance"/> is null.</exception>
        internal Instance(ISetupInstance2 instance)
        {
            Validate.NotNull(instance, nameof(instance));

            // The instance ID is required, but then try to set other properties to release the COM object and its resources ASAP.
            InstanceId = instance.GetInstanceId();

            Utilities.TrySet(ref installationName, nameof(InstallationName), instance.GetInstallationName, OnError);
            Utilities.TrySet(ref installationPath, nameof(InstallationPath), instance.GetInstallationPath, OnError);
            Utilities.TrySet(
                ref installationVersion,
                nameof(InstallationVersion),
                () =>
            {
                var versionString = instance.GetInstallationVersion();
                if (Utilities.TryParseVersion(versionString, out Version version))
                {
                    return(version.Normalize());
                }

                return(null);
            },
                OnError);

            Utilities.TrySet(
                ref installDate,
                nameof(InstallDate),
                () =>
            {
                var ft = instance.GetInstallDate();
                var l  = ((long)ft.dwHighDateTime << 32) + ft.dwLowDateTime;

                return(DateTime.FromFileTime(l));
            },
                OnError);

            Utilities.TrySet(ref state, nameof(State), instance.GetState, OnError);

            var lcid = CultureInfo.CurrentUICulture.LCID;

            Utilities.TrySet(
                ref displayName,
                nameof(DisplayName),
                () =>
            {
                return(instance.GetDisplayName(lcid));
            },
                OnError);

            Utilities.TrySet(
                ref description,
                nameof(Description),
                () =>
            {
                return(instance.GetDescription(lcid));
            },
                OnError);

            Utilities.TrySet(
                ref productPath,
                nameof(ProductPath),
                () =>
            {
                var path = instance.GetProductPath();
                return(instance.ResolvePath(path));
            },
                OnError);

            Utilities.TrySet(
                ref product,
                nameof(Product),
                () =>
            {
                var reference = instance.GetProduct();
                if (reference != null)
                {
                    return(new PackageReference(reference));
                }

                return(null);
            },
                OnError);

            Packages = Utilities.TrySetCollection(ref packages, nameof(Packages), instance.GetPackages, PackageReferenceFactory.Create, OnError);

            var errors = instance.GetErrors();

            if (errors != null)
            {
                Errors = new Errors(errors);
            }

            Utilities.TrySet(
                ref properties,
                nameof(Properties),
                () =>
            {
                var properties = instance.GetProperties();
                return(properties?.GetNames()
                       .ToDictionary(name => name.ToPascalCase(), name => properties.GetValue(name), StringComparer.OrdinalIgnoreCase));
            },
                OnError);

            if (properties != null)
            {
                Properties = new ReadOnlyDictionary <string, object>(properties);
            }
            else
            {
                // While accessing properties on a null object succeeds in PowerShell, accessing the indexer does not.
                Properties = ReadOnlyDictionary <string, object> .Empty;
            }

            Utilities.TrySet(ref enginePath, nameof(EnginePath), instance.GetEnginePath, OnError);
            Utilities.TrySet(ref isComplete, nameof(IsComplete), instance.IsComplete, OnError);
            Utilities.TrySet(ref isLaunchable, nameof(IsLaunchable), instance.IsLaunchable, OnError);

            Utilities.TrySet(
                ref catalogInfo,
                nameof(CatalogInfo),
                () =>
            {
                var catalog    = instance as ISetupInstanceCatalog;
                var properties = catalog?.GetCatalogInfo();
                return(properties?.GetNames()
                       .ToDictionary(name => name.ToPascalCase(), name => properties.GetValue(name), StringComparer.OrdinalIgnoreCase));
            },
                OnError);

            if (catalogInfo != null)
            {
                CatalogInfo = new ReadOnlyDictionary <string, object>(catalogInfo);
            }
            else
            {
                // While accessing properties on a null object succeeds in PowerShell, accessing the indexer does not.
                CatalogInfo = ReadOnlyDictionary <string, object> .Empty;
            }

            Utilities.TrySet(
                ref isPrerelease,
                nameof(IsPrerelease),
                () =>
            {
                var catalog = instance as ISetupInstanceCatalog;
                return(catalog?.IsPrerelease());
            },
                OnError);

            // Get all properties of the instance not explicitly declared.
            var store = (ISetupPropertyStore)instance;

            AdditionalProperties = store.GetNames()
                                   .Where(name => !DeclaredProperties.Contains(name))
                                   .ToDictionary(name => name.ToPascalCase(), name => store.GetValue(name), StringComparer.OrdinalIgnoreCase);
        }