Ejemplo n.º 1
0
        /// <summary>
        /// Parses and returns an indexer.
        /// </summary>
        /// <param name="parent">
        /// The parent of the element.
        /// </param>
        /// <param name="elementReference">
        /// A reference to the element being created.
        /// </param>
        /// <param name="unsafeCode">
        /// Indicates whether the code is marked as unsafe.
        /// </param>
        /// <param name="generated">
        /// Indicates whether the code is marked as generated code.
        /// </param>
        /// <param name="xmlHeader">
        /// The element's documentation header.
        /// </param>
        /// <param name="attributes">
        /// The attributes on the element.
        /// </param>
        /// <returns>
        /// Returns the element.
        /// </returns>
        private Indexer ParseIndexer(
            CsElement parent, Reference<ICodePart> elementReference, bool unsafeCode, bool generated, XmlHeader xmlHeader, ICollection<Attribute> attributes)
        {
            Param.AssertNotNull(parent, "parent");
            Param.AssertNotNull(elementReference, "elementReference");
            Param.Ignore(unsafeCode);
            Param.Ignore(generated);
            Param.Ignore(xmlHeader);
            Param.Ignore(attributes);

            Node<CsToken> previousTokenNode = this.tokens.Last;

            // Get the modifiers and access.
            AccessModifierType accessModifier = AccessModifierType.Private;

            // Indexers within interfaces always have the access of the parent interface.
            Interface parentInterface = parent as Interface;
            if (parentInterface != null)
            {
                accessModifier = parentInterface.AccessModifier;
            }

            // Get declared modifiers.
            Dictionary<CsTokenType, CsToken> modifiers = this.GetElementModifiers(elementReference, ref accessModifier, IndexerModifiers);

            unsafeCode |= modifiers.ContainsKey(CsTokenType.Unsafe);

            // Get the return type.
            TypeToken returnType = this.GetTypeToken(elementReference, unsafeCode, true);
            this.tokens.Add(returnType);

            // Get the name of the indexer.
            CsToken name = this.GetElementNameToken(elementReference, unsafeCode);
            this.tokens.Add(name);

            // Get the parameter list.
            IList<Parameter> parameters = this.ParseParameterList(elementReference, unsafeCode, SymbolType.OpenSquareBracket);

            // Create the declaration.
            Node<CsToken> firstTokenNode = previousTokenNode == null ? this.tokens.First : previousTokenNode.Next;
            CsTokenList declarationTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last);
            Declaration declaration = new Declaration(declarationTokens, name.Text, ElementType.Indexer, accessModifier, modifiers);

            Indexer indexer = new Indexer(this.document, parent, xmlHeader, attributes, declaration, returnType, parameters, unsafeCode, generated);
            elementReference.Target = indexer;

            // Parse the body of the indexer.
            this.ParseElementContainer(indexer, elementReference, null, unsafeCode);

            return indexer;
        }
        /// <summary>
        /// The save.
        /// </summary>
        /// <param name="indexer">
        /// The indexer.
        /// </param>
        private void Save(Indexer @indexer)
        {
            var @propertyName = @indexer.Declaration.Name;
            this.headerWriter.Write(
                string.Format(
                    "__declspec(property({0}{2}{1})) ",
                    (@indexer.GetAccessor != null)
                        ? string.Concat("get = ", this.GetFunctionPrefix(@indexer.GetAccessor), "this")
                        : string.Empty,
                    (@indexer.SetAccessor != null)
                        ? string.Concat("put = ", this.GetFunctionPrefix(@indexer.SetAccessor), "this")
                        : string.Empty,
                    (@indexer.GetAccessor != null && @indexer.SetAccessor != null) ? ", " : string.Empty));

            if (@indexer.ReturnType != null)
            {
                // 1 to destHeader
                this.Save(@indexer.ReturnType);
            }

            // 1 to destHeader
            this.headerWriter.Write(" ");

            // this.destHeader.Write(@indexer.Declaration.Name);
            this.headerWriter.Write("Default");
            this.headerWriter.WriteLine("[];");

            if (@indexer.GetAccessor != null)
            {
                this.Save(@indexer.GetAccessor);
            }

            if (@indexer.SetAccessor != null)
            {
                this.Save(@indexer.SetAccessor);
            }
        }