internal Instance()

in src/VSSetup.PowerShell/Instance.cs [55:201]


        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 var 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),
                () => instance.GetDisplayName(lcid),
                OnError);

            Utilities.TrySet(
                ref description,
                nameof(Description),
                () => 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);
        }