示例#1
0
        /// <summary>
        /// Returns those XAML namespace definitions for which a conditional is set, grouped by those for which the conditional returns true and
        /// should be included, and those for which it returns fales and should be excluded.
        /// </summary>
        private (List <XmlAttribute> IncludedConditionals, List <XmlAttribute> ExcludedConditionals) FindConditionals(XmlDocument document)
        {
            var included = new List <XmlAttribute>();
            var excluded = new List <XmlAttribute>();

            foreach (XmlAttribute attr in document.DocumentElement.Attributes)
            {
                if (attr.Prefix != "xmlns")
                {
                    // Not a namespace
                    continue;
                }

                var valueSplit = attr.Value.Split('?');
                if (valueSplit.Length != 2)
                {
                    // Not a (valid) conditional
                    continue;
                }

                if (ShouldInclude() is bool shouldInclude)
                {
                    if (shouldInclude)
                    {
                        included.Add(attr);
                    }
                    else
                    {
                        excluded.Add(attr);
                    }
                }

                bool?ShouldInclude()
                {
                    var elements = valueSplit[1].Split('(', ',', ')');

                    switch (elements[0])
                    {
                    case nameof(ApiInformation.IsApiContractPresent):
                    case nameof(ApiInformation.IsApiContractNotPresent):
                        if (elements.Length < 4 || !ushort.TryParse(elements[2].Trim(), out var majorVersion))
                        {
                            throw new InvalidOperationException($"Syntax error while parsing conditional namespace expression {attr.Value}");
                        }

                        return(elements[0] == nameof(ApiInformation.IsApiContractPresent) ?
                               ApiInformation.IsApiContractPresent(elements[1], majorVersion) :
                               ApiInformation.IsApiContractNotPresent(elements[1], majorVersion));

                    default:
                        return(null);                               // TODO: support IsTypePresent and IsPropertyPresent
                    }
                }
            }

            return(included, excluded);
        }