當前位置: 首頁>>代碼示例>>C#>>正文


C# CSharpParser.Parse方法代碼示例

本文整理匯總了C#中NArrange.CSharp.CSharpParser.Parse方法的典型用法代碼示例。如果您正苦於以下問題:C# CSharpParser.Parse方法的具體用法?C# CSharpParser.Parse怎麽用?C# CSharpParser.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在NArrange.CSharp.CSharpParser的用法示例。


在下文中一共展示了CSharpParser.Parse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: TestAutoPropertyInitializersParseCorrectly

 public void TestAutoPropertyInitializersParseCorrectly()
 {
     CSharpTestFile testFile = CSharpTestUtilities.GetAutoPropertyInitializersFile();
     using (TextReader reader = testFile.GetReader())
     {
         CSharpParser parser = new CSharpParser();
         ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);
         elements.Should().HaveCount(1, "because there is only one namespace");
         elements[0].Children.Should().HaveCount(1, "because there is 1 class in the namespace");
         elements[0].Children[0].Children.Should().HaveCount(4, "because there are 4 subclasses in the class");
         var customer1 = elements[0].Children[0].Children[0];
         var customer2 = elements[0].Children[0].Children[1];
         var customer3 = elements[0].Children[0].Children[2];
         var customer4 = elements[0].Children[0].Children[3];
         customer1.Children.Should().HaveCount(2, "because there are only 2 properties");
         customer1.Children[0].ElementType.Should().Be(ElementType.Property);
         customer1.Children[1].ElementType.Should().Be(ElementType.Property);
         customer2.Children.Should().HaveCount(2, "because there are only 2 properties");
         customer2.Children[0].ElementType.Should().Be(ElementType.Property);
         customer2.Children[1].ElementType.Should().Be(ElementType.Property);
         customer3.Children.Should().HaveCount(2, "because there is only 1 property and 1 constructor");
         customer3.Children[0].ElementType.Should().Be(ElementType.Property);
         customer3.Children[1].ElementType.Should().Be(ElementType.Constructor);
         customer4.Children.Should().HaveCount(2, "because there are only 2 properties");
         customer4.Children[0].ElementType.Should().Be(ElementType.Property);
         customer4.Children[1].ElementType.Should().Be(ElementType.Property);
     }
 }
開發者ID:MarcStan,項目名稱:NArrange,代碼行數:28,代碼來源:CSharp6FeatureTests.cs

示例2: ExpectedBlockCloseTest

        public void ExpectedBlockCloseTest()
        {
            StringReader reader = new StringReader(
                "namespace SampleNamespace\r\n{");

            CSharpParser parser = new CSharpParser();
            parser.Parse(reader);
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:8,代碼來源:CSharpParserTests.cs

示例3: ParseClassPartialUnspecifiedAccessTest

        public void ParseClassPartialUnspecifiedAccessTest()
        {
            StringReader reader = new StringReader(
                "partial class Test{}");

            CSharpParser parser = new CSharpParser();
            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);

            Assert.AreEqual(1, elements.Count, "An unexpected number of elements were parsed.");
            TypeElement typeElement = elements[0] as TypeElement;
            Assert.IsNotNull(typeElement, "Element is not a TypeElement.");
            Assert.AreEqual("Test", typeElement.Name, "Unexpected name.");
            Assert.AreEqual(CodeAccess.None, typeElement.Access, "Unexpected code access.");
            Assert.IsTrue(typeElement.IsPartial, "Expected a partial class.");
            Assert.AreEqual(TypeElementType.Class, typeElement.Type, "Unexpected type element type.");
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:16,代碼來源:CSharpParserTests.cs

示例4: ParseUsingExpectedStatementEnd

        public void ParseUsingExpectedStatementEnd()
        {
            StringReader reader = new StringReader(
                "using System.Text");

            CSharpParser parser = new CSharpParser();
            parser.Parse(reader);
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:8,代碼來源:CSharpParserTests.cs

示例5: ParseClassNewConstraintOrderTest

        public void ParseClassNewConstraintOrderTest()
        {
            StringReader reader = new StringReader(
                "public class Test<T> where T : new(), IDisposable {}");

            CSharpParser parser = new CSharpParser();
            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:8,代碼來源:CSharpParserTests.cs

示例6: ParseCommentBlockTest

        public void ParseCommentBlockTest()
        {
            StringReader reader = new StringReader(
                "/*\r\n" +
                " * Block comment here\r\n" +
                " */\r\n");

            CSharpParser parser = new CSharpParser();
            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);

            Assert.AreEqual(1, elements.Count, "An unexpected number of elements were parsed.");
            CommentElement commentBlockElement = elements[0] as CommentElement;
            Assert.AreEqual(CommentType.Block, commentBlockElement.Type, "Element is not a CommentBlockElement.");

            string[] lines = commentBlockElement.Text.Split(
                new string[] { Environment.NewLine }, StringSplitOptions.None);
            Assert.AreEqual(3, lines.Length, "An unexpected number of comment lines were parsed.");
            Assert.AreEqual(string.Empty, lines[0], "Unexpected comment line at index 0.");
            Assert.AreEqual(" * Block comment here", lines[1], "Unexpected comment line at index 1.");
            Assert.AreEqual(" ", lines[2], "Unexpected comment line at index 2.");
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:21,代碼來源:CSharpParserTests.cs

示例7: ParseUsingTest

        public void ParseUsingTest()
        {
            StringReader reader = new StringReader(
                "using System.Text;");

            CSharpParser parser = new CSharpParser();
            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);

            Assert.AreEqual(1, elements.Count, "An unexpected number of elements were parsed.");
            UsingElement usingElement = elements[0] as UsingElement;
            Assert.IsNotNull(usingElement, "Element is not a UsingElement.");
            Assert.AreEqual("System.Text", usingElement.Name, "Unexpected name.");
            Assert.IsTrue(usingElement.IsMovable, "C# should support moving using directives.");
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:14,代碼來源:CSharpParserTests.cs

示例8: ExpectedFieldEndOfStatementTest

        public void ExpectedFieldEndOfStatementTest()
        {
            StringReader reader = new StringReader(
                "namespace SampleNamespace\r\n" +
                "{\r\n" +
                "    public class SampleClass\r\n" +
                "    {\r\n" +
                "        private string test =\r\n" +
                "    }\r\n" +
                "}");

            CSharpParser parser = new CSharpParser();
            parser.Parse(reader);
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:14,代碼來源:CSharpParserTests.cs

示例9: ParseClassUnknownTypeParameterTest

        public void ParseClassUnknownTypeParameterTest()
        {
            StringReader reader = new StringReader(
                "public class Test<T> where S : new() {}");

            CSharpParser parser = new CSharpParser();
            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:8,代碼來源:CSharpParserTests.cs

示例10: ParseClassDefinitionTest

        public void ParseClassDefinitionTest()
        {
            CSharpParser parser = new CSharpParser();

            CSharpTestFile testFile = CSharpTestUtilities.GetClassDefinitionFile();
            using (TextReader reader = testFile.GetReader())
            {
                ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);

                Assert.IsNotNull(elements, "Code element collection should not be null.");
                Assert.AreEqual(8, elements.Count, "An unexpected number of elements were parsed.");

                CommentElement commentElement = elements[0] as CommentElement;
                Assert.IsNotNull(commentElement, "Expected a CommentElement.");

                CommentElement commentElement1 = elements[1] as CommentElement;
                Assert.IsNotNull(commentElement1, "Expected a CommentElement.");
                Assert.AreEqual(" This is comment line 1", commentElement1.Text, "Unexpected comment text.");

                CommentElement commentElement2 = elements[2] as CommentElement;
                Assert.IsNotNull(commentElement2, "Expected a CommentElement.");
                Assert.AreEqual(" This is comment line 2", commentElement2.Text, "Unexpected comment text.");

                CommentElement commentElement3 = elements[3] as CommentElement;
                Assert.IsNotNull(commentElement3, "Expected a CommentElement.");
                Assert.AreEqual(" This is comment line 3", commentElement3.Text, "Unexpected comment text.");

                UsingElement using1 = elements[4] as UsingElement;
                Assert.IsNotNull(using1, "Expected a UsingElement.");
                Assert.AreEqual("System", using1.Name, "Unexpected using name.");

                UsingElement using2 = elements[5] as UsingElement;
                Assert.IsNotNull(using2, "Expected a UsingElement.");
                Assert.AreEqual("System.Collections.Generic", using2.Name, "Unexpected using name.");

                UsingElement using3 = elements[6] as UsingElement;
                Assert.IsNotNull(using3, "Expected a UsingElement.");
                Assert.AreEqual("System.Text", using3.Name, "Unexpected using name.");

                NamespaceElement namespaceElement = elements[7] as NamespaceElement;
                Assert.IsNotNull(namespaceElement, "Expected a NamespaceElement.");
                Assert.AreEqual("SampleNamespace", namespaceElement.Name, "Unexpected namespace name.");

                Assert.IsNotNull(namespaceElement.Children, "Namespace Children collection should not be null.");
                Assert.AreEqual(1, namespaceElement.Children.Count, "An unexpected number of namespace child elements were parsed.");

                TypeElement classElement = namespaceElement.Children[0] as TypeElement;
                Assert.IsNotNull(classElement, "Expected a TypeElement.");
                Assert.AreEqual("SampleClass", classElement.Name, "Unexpected class name.");
                Assert.AreEqual(3, classElement.HeaderComments.Count, "An unexpected number of class header comment lines were parsed.");
                foreach (ICommentElement comment in
                    classElement.HeaderComments)
                {
                    Assert.AreEqual(CommentType.XmlLine, comment.Type, "Class header comment should be an XML comment.");
                }
                Assert.AreEqual(CodeAccess.Public, classElement.Access, "Unexpected class code access level.");
            }
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:58,代碼來源:CSharpParserTests.cs

示例11: ParseClassEmptyParameterConstraintListTest

        public void ParseClassEmptyParameterConstraintListTest()
        {
            StringReader reader = new StringReader(
                "public class Test<T> where T : {}");

            CSharpParser parser = new CSharpParser();
            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:8,代碼來源:CSharpParserTests.cs

示例12: ParseAttributeWithAttributeCharacterTest

        public void ParseAttributeWithAttributeCharacterTest()
        {
            StringReader reader = new StringReader(
                "[assembly: AssemblyDescription(\"SampleAssembly]\")]");

            CSharpParser parser = new CSharpParser();
            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);

            Assert.AreEqual(1, elements.Count, "An unexpected number of elements were parsed.");
            AttributeElement attributeElement = elements[0] as AttributeElement;
            Assert.IsNotNull(attributeElement, "Element is not a AttributeElement.");
            Assert.AreEqual("assembly", attributeElement.Target, "Unexpected attribute target.");
            Assert.AreEqual("AssemblyDescription", attributeElement.Name, "Unexpected attribute name.");
            Assert.AreEqual("\"SampleAssembly]\"", attributeElement.BodyText, "Unexpected attribute text.");
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:15,代碼來源:CSharpParserTests.cs

示例13: ParseBodyEscapedStringTest

        public void ParseBodyEscapedStringTest()
        {
            StringReader reader = new StringReader(
                "public void DoSomething()\r\n" +
                "{\r\n" +
                "\tstring v = string.Empty;\r\n" +
                "\tv = v.Replace(\"\\\\\\\"\", \"\\\"\"\r\n" +
                "}");

            CSharpParser parser = new CSharpParser();
            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);

            Assert.AreEqual(1, elements.Count, "Unexpected number of elements were parsed.");
            MethodElement methodElement = elements[0] as MethodElement;
            Assert.IsNotNull(methodElement);

            Assert.IsTrue(
                methodElement.BodyText.Contains("v = v.Replace(\"\\\\\\\"\", \"\\\"\""),
                "Escaped string line was not found in the member body.");
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:20,代碼來源:CSharpParserTests.cs

示例14: ParseAttributeListTest

        public void ParseAttributeListTest()
        {
            StringReader reader = new StringReader(
                "[assembly: AssemblyDescription(\"SampleAssembly\"), ComVisible(false),\tAssemblyConfiguration(\"\"), TestAttribute]");

            CSharpParser parser = new CSharpParser();
            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);

            Assert.AreEqual(1, elements.Count, "An unexpected number of elements were parsed.");
            AttributeElement attributeElement;

            attributeElement = elements[0] as AttributeElement;
            Assert.IsNotNull(attributeElement, "Element is not an AttributeElement.");
            Assert.AreEqual("assembly", attributeElement.Target, "Unexpected attribute target.");
            Assert.AreEqual("AssemblyDescription", attributeElement.Name, "Unexpected attribute name.");
            Assert.AreEqual("\"SampleAssembly\"", attributeElement.BodyText, "Unexpected attribute text.");

            Assert.AreEqual(3, attributeElement.Children.Count, "An unexpected number of child elements were parsed.");

            AttributeElement attributeChildElement = attributeElement.Children[0] as AttributeElement;
            Assert.IsNotNull(attributeChildElement, "Element is not an AttributeElement.");
            Assert.AreEqual("assembly", attributeChildElement.Target, "Unexpected attribute target.");
            Assert.AreEqual("ComVisible", attributeChildElement.Name, "Unexpected attribute name.");
            Assert.AreEqual("false", attributeChildElement.BodyText, "Unexpected attribute text.");

            attributeChildElement = attributeElement.Children[1] as AttributeElement;
            Assert.IsNotNull(attributeChildElement, "Element is not an AttributeElement.");
            Assert.AreEqual("assembly", attributeChildElement.Target, "Unexpected attribute target.");
            Assert.AreEqual("AssemblyConfiguration", attributeChildElement.Name, "Unexpected attribute name.");
            Assert.AreEqual("\"\"", attributeChildElement.BodyText, "Unexpected attribute text.");

            attributeChildElement = attributeElement.Children[2] as AttributeElement;
            Assert.IsNotNull(attributeChildElement, "Element is not an AttributeElement.");
            Assert.AreEqual("assembly", attributeChildElement.Target, "Unexpected attribute target.");
            Assert.AreEqual("TestAttribute", attributeChildElement.Name, "Unexpected attribute name.");
            Assert.IsNull(attributeChildElement.BodyText, "Unexpected attribute text.");
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:37,代碼來源:CSharpParserTests.cs

示例15: GetMembersTestClass

        /// <summary>
        /// Gets the ClassMembers test class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns>Type code element.</returns>
        private static TypeElement GetMembersTestClass(TextReader reader)
        {
            TypeElement classElement;
            CSharpParser parser = new CSharpParser();

            ReadOnlyCollection<ICodeElement> elements = parser.Parse(reader);

            Assert.AreEqual(9, elements.Count, "Unexpected number of top-level elements.");

            NamespaceElement namespaceElement = elements[8] as NamespaceElement;
            Assert.IsNotNull(namespaceElement, "Expected a namespace element.");

            Assert.AreEqual(2, namespaceElement.Children.Count, "Unexpected number of namespace elements.");

            UsingElement usingElement = namespaceElement.Children[0] as UsingElement;
            Assert.IsNotNull(usingElement, "Expected a using element.");
            Assert.AreEqual("System.ComponentModel", usingElement.Name, "Unexpected using element name.");

            classElement = namespaceElement.Children[1] as TypeElement;
            Assert.IsNotNull(classElement, "Expected a type element.");
            Assert.AreEqual(TypeElementType.Class, classElement.Type, "Expected a class type.");
            Assert.AreEqual("SampleClass", classElement.Name, "Unexpected class name.");
            Assert.AreEqual(4, classElement.HeaderComments.Count, "Unexpected number of header comments.");

            return classElement;
        }
開發者ID:samuel-weber,項目名稱:NArrange,代碼行數:31,代碼來源:CSharpParserTests.cs


注:本文中的NArrange.CSharp.CSharpParser.Parse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。