/// <summary>
        /// Initializes a new instance of the <see cref="PackageReference"/> class.
        /// </summary>
        /// <param name="reference">The <see cref="ISetupPackageReference"/> to adapt.</param>
        /// <exception cref="ArgumentNullException"><paramref name="reference"/> is null.</exception>
        internal PackageReference(ISetupPackageReference reference)
        {
            Validate.NotNull(reference, nameof(reference));

            // The package reference ID is required, but then try to set other properties to release the COM object and its resources ASAP.
            Id = reference.GetId();

            Utilities.TrySet(ref version, nameof(Version), () =>
            {
                var versionString = reference.GetVersion();
                if (Utilities.TryParseVersion(versionString, out Version version))
                {
                    return(version);
                }

                return(null);
            });

            Utilities.TrySet(ref chip, nameof(Chip), reference.GetChip);
            Utilities.TrySet(ref branch, nameof(Branch), reference.GetBranch);
            Utilities.TrySet(ref type, nameof(Type), reference.GetType);
            Utilities.TrySet(ref isExtension, nameof(IsExtension), reference.GetIsExtension);
            Utilities.TrySet(ref uniqueId, nameof(UniqueId), reference.GetUniqueId);
        }
        /// <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>());
        }
Example #3
0
 public void NotNull()
 {
     Validate.NotNull("test", "test");
 }
Example #4
0
 public void NotNull_Throws()
 {
     Assert.Throws <ArgumentNullException>("test", () => Validate.NotNull <object>(null, "test"));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FailedPackageReference"/> class.
 /// </summary>
 /// <param name="reference">The <see cref="ISetupFailedPackageReference"/> to adapt.</param>
 /// <exception cref="ArgumentNullException"><paramref name="reference"/> is null.</exception>
 internal FailedPackageReference(ISetupFailedPackageReference reference)
     : base(reference)
 {
     Validate.NotNull(reference, nameof(reference));
 }
        /// <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 #7
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();

            TrySet(ref installationName, nameof(InstallationName), instance.GetInstallationName);
            TrySet(ref installationPath, nameof(InstallationPath), instance.GetInstallationPath);
            TrySet(ref installationVersion, nameof(InstallationVersion), () =>
            {
                Version version;

                var versionString = instance.GetInstallationVersion();
                if (Version.TryParse(versionString, out version))
                {
                    return(version.Normalize());
                }

                return(null);
            });

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

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

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

            var lcid = CultureInfo.CurrentUICulture.LCID;

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

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

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

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

                return(null);
            });

            TrySet(ref packages, nameof(Packages), () =>
            {
                return(new List <PackageReference>(GetPackages(instance)));
            });

            if (packages != null && packages.Any())
            {
                Packages = new ReadOnlyCollection <PackageReference>(packages);
            }

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

            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;
            }

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

            // 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);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="Lazy{T}"/> class.
 /// </summary>
 /// <param name="factory">A <see cref="Func{TResult}"/> to create an instance of the type.</param>
 public Lazy(Func <T> factory)
 {
     Validate.NotNull(factory, nameof(factory));
     this.factory = factory;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ReadOnlyDictionary{TKey, TValue}"/> class.
        /// </summary>
        /// <param name="dictionary">The dictionary to wrap.</param>
        /// <exception cref="ArgumentNullException"><paramref name="dictionary"/> is null.</exception>
        public ReadOnlyDictionary(IDictionary <TKey, TValue> dictionary)
        {
            Validate.NotNull(dictionary, nameof(dictionary));

            this.dictionary = dictionary;
        }