internal IntPtr ToNative(PinCollection pinCollection)
        {
            var nativeDescription = new NativeTypes.FABRIC_APPLICATION_UPGRADE_DESCRIPTION();

            nativeDescription.ApplicationName = pinCollection.AddObject(this.ApplicationName);
            nativeDescription.TargetApplicationTypeVersion = pinCollection.AddBlittable(this.TargetApplicationTypeVersion);

            if (this.ApplicationParameters.Count != 0)
            {
                var applicationParameterList = new ApplicationParameterList(this.ApplicationParameters);
                nativeDescription.ApplicationParameters = applicationParameterList.ToNative(pinCollection);
            }
            else
            {
                nativeDescription.ApplicationParameters = IntPtr.Zero;
            }

            var rollingUpgradePolicyDescription = this.UpgradePolicyDescription as RollingUpgradePolicyDescription;

            if (rollingUpgradePolicyDescription != null)
            {
                nativeDescription.UpgradeKind = NativeTypes.FABRIC_APPLICATION_UPGRADE_KIND.FABRIC_APPLICATION_UPGRADE_KIND_ROLLING;
                nativeDescription.UpgradePolicyDescription = rollingUpgradePolicyDescription.ToNative(pinCollection);
            }
            else
            {
                throw new ArgumentException("description.UpgradePolicyDescription");
            }

            return(pinCollection.AddBlittable(nativeDescription));
        }
        internal IntPtr ToNative(PinCollection pinCollection)
        {
            var nativeDescription = new NativeTypes.FABRIC_APPLICATION_DESCRIPTION();

            nativeDescription.ApplicationName        = pinCollection.AddObject(this.ApplicationName);
            nativeDescription.ApplicationTypeName    = pinCollection.AddObject(this.ApplicationTypeName);
            nativeDescription.ApplicationTypeVersion = pinCollection.AddObject(this.ApplicationTypeVersion);

            if (this.ApplicationParameters.Count != 0)
            {
                var applicationParameterList = new ApplicationParameterList(this.ApplicationParameters);
                nativeDescription.ApplicationParameters = applicationParameterList.ToNative(pinCollection);
            }
            else
            {
                nativeDescription.ApplicationParameters = IntPtr.Zero;
            }

            if (this.MaximumNodes > 0 || this.MinimumNodes > 0 || this.Metrics != null)
            {
                var nativeDescriptionEx1 = new NativeTypes.FABRIC_APPLICATION_DESCRIPTION_EX1();
                var nativeApplicationCapacityDescription = new NativeTypes.FABRIC_APPLICATION_CAPACITY_DESCRIPTION();
                nativeApplicationCapacityDescription.MaximumNodes = checked ((uint)this.MaximumNodes);
                nativeApplicationCapacityDescription.MinimumNodes = checked ((uint)this.MinimumNodes);

                var nativeMetricsList = new NativeTypes.FABRIC_APPLICATION_METRIC_LIST();
                if (null != this.Metrics)
                {
                    nativeMetricsList.Count = (uint)this.Metrics.Count;
                    if (this.Metrics.Count > 0)
                    {
                        var nativeMetricsArray = new NativeTypes.FABRIC_APPLICATION_METRIC_DESCRIPTION[this.Metrics.Count];
                        for (int i = 0; i < this.Metrics.Count; i++)
                        {
                            Metrics[i].ToNative(pinCollection, ref nativeMetricsArray[i]);
                        }

                        nativeMetricsList.Metrics = pinCollection.AddBlittable(nativeMetricsArray);
                    }
                    else
                    {
                        nativeMetricsList.Metrics = IntPtr.Zero;
                    }
                }
                else
                {
                    nativeMetricsList.Count   = 0;
                    nativeMetricsList.Metrics = IntPtr.Zero;
                }
                nativeApplicationCapacityDescription.Metrics = pinCollection.AddBlittable(nativeMetricsList);

                nativeDescriptionEx1.ApplicationCapacity = pinCollection.AddBlittable(nativeApplicationCapacityDescription);

                nativeDescription.Reserved = pinCollection.AddBlittable(nativeDescriptionEx1);
            }

            return(pinCollection.AddBlittable(nativeDescription));
        }
        internal static unsafe ApplicationParameterList FromNative(
            NativeTypes.FABRIC_APPLICATION_PARAMETER_LIST *nativeList)
        {
            var retval = new ApplicationParameterList();

            var nativeItemArray = (NativeTypes.FABRIC_APPLICATION_PARAMETER *)nativeList->Items;

            for (int i = 0; i < nativeList->Count; ++i)
            {
                var nativeItem = *(nativeItemArray + i);
                retval.Add(ApplicationParameter.CreateFromNative(nativeItem));
            }

            return(retval);
        }
        internal static unsafe ApplicationUpgradeDescription FromNative(IntPtr descriptionPtr)
        {
            if (descriptionPtr == IntPtr.Zero)
            {
                return(null);
            }

            var castedPtr = (NativeTypes.FABRIC_APPLICATION_UPGRADE_DESCRIPTION *)descriptionPtr;

            var description = new ApplicationUpgradeDescription();

            description.ApplicationName = NativeTypes.FromNativeUri(castedPtr->ApplicationName);
            description.TargetApplicationTypeVersion = NativeTypes.FromNativeString(castedPtr->TargetApplicationTypeVersion);

            if (castedPtr->ApplicationParameters != IntPtr.Zero)
            {
                var castedParamsPtr = (NativeTypes.FABRIC_APPLICATION_PARAMETER_LIST *)castedPtr->ApplicationParameters;
                var parametersList  = ApplicationParameterList.FromNative(castedParamsPtr);
                description.ApplicationParameters = parametersList.AsNameValueCollection();
            }

            if (castedPtr->UpgradeKind == NativeTypes.FABRIC_APPLICATION_UPGRADE_KIND.FABRIC_APPLICATION_UPGRADE_KIND_ROLLING &&
                castedPtr->UpgradePolicyDescription != IntPtr.Zero)
            {
                RollingUpgradePolicyDescription policy = null;

                var castedPolicyPtr = (NativeTypes.FABRIC_ROLLING_UPGRADE_POLICY_DESCRIPTION *)castedPtr->UpgradePolicyDescription;

                if (castedPolicyPtr->RollingUpgradeMode == NativeTypes.FABRIC_ROLLING_UPGRADE_MODE.FABRIC_ROLLING_UPGRADE_MODE_MONITORED)
                {
                    policy = MonitoredRollingApplicationUpgradePolicyDescription.FromNative(castedPtr->UpgradePolicyDescription);
                }
                else
                {
                    policy = RollingUpgradePolicyDescription.FromNative(castedPtr->UpgradePolicyDescription);
                }

                description.UpgradePolicyDescription = policy;
            }

            return(description);
        }