当前位置: 首页>>代码示例>>C#>>正文


C# CodeDomProvider.GenerateCodeFromStatement方法代码示例

本文整理汇总了C#中CodeDomProvider.GenerateCodeFromStatement方法的典型用法代码示例。如果您正苦于以下问题:C# CodeDomProvider.GenerateCodeFromStatement方法的具体用法?C# CodeDomProvider.GenerateCodeFromStatement怎么用?C# CodeDomProvider.GenerateCodeFromStatement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CodeDomProvider的用法示例。


在下文中一共展示了CodeDomProvider.GenerateCodeFromStatement方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BuildTree


//.........这里部分代码省略.........
            //               return iReturn;
            //       }
            //   }

            CodeNamespace ns = new CodeNamespace ("Foo");
            ns.Comments.Add (new CodeCommentStatement ("Foo namespace"));

            cu.Namespaces.Add (ns);

            CodeTypeDeclaration cd = new CodeTypeDeclaration ("Foo");
            ns.Types.Add (cd);

            CodeMemberProperty property = new CodeMemberProperty ();
            property.Name = "System";
            property.Attributes = MemberAttributes.Public;
            property.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private | MemberAttributes.Static;
            property.Type = new CodeTypeReference (typeof (int));
            property.GetStatements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (42)));
            cd.Members.Add (property);

            property = new CodeMemberProperty ();
            property.Name = "Property";
            property.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private | MemberAttributes.Static;
            property.Type = new CodeTypeReference (typeof (int));
            property.GetStatements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (Int32.MaxValue)));
            cd.Members.Add (property);

            AddScenario ("CallTestMethod02", "Call Foo.Foo.TestMethod02.");
            CodeMemberMethod method2 = new CodeMemberMethod ();
            method2.Name = "TestMethod02";
            method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method2.ReturnType = new CodeTypeReference (typeof (int));
            method2.Statements.Add (new CodeVariableDeclarationStatement (typeof (int), "iReturn"));

            CodePropertyReferenceExpression cpr = new CodePropertyReferenceExpression (
                                              new CodeTypeReferenceExpression (new CodeTypeReference ("Foo.Foo",
                                                      CodeTypeReferenceOptions.GlobalReference)), "Property");

            CodeAssignStatement cas = new CodeAssignStatement (new CodeVariableReferenceExpression ("iReturn"), cpr);
            method2.Statements.Add (cas);
            method2.Statements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("iReturn")));
            cd.Members.Add (method2);

            AddScenario ("CallTestMethod03", "Call Foo.Foo.TestMethod02.");
            CodeMemberMethod method3 = new CodeMemberMethod ();
            method3.Name = "TestMethod03";
            method3.Attributes = (method3.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method3.ReturnType = new CodeTypeReference (typeof (int));
            method3.Statements.Add (new CodeVariableDeclarationStatement (typeof (int), "iReturn"));
            CodeTypeReferenceOptions ctro = CodeTypeReferenceOptions.GlobalReference;
            CodeTypeReference ctr = new CodeTypeReference (typeof (Math), ctro);
            CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression (
                                              new CodeMethodReferenceExpression (
                                              new CodeTypeReferenceExpression (ctr), "Abs"), new CodeExpression[] { new CodePrimitiveExpression (-1) });
            cas = new CodeAssignStatement (new CodeVariableReferenceExpression ("iReturn"), cmie);
            method3.Statements.Add (cas);
            method3.Statements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("iReturn")));
            cd.Members.Add (method3);

            AddScenario ("CallTestMethod04", "Call Foo.Foo.TestMethod04.");
            CodeMemberMethod method4 = new CodeMemberMethod ();
            method4.Name = "TestMethod04";
            method4.Attributes = (method4.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method4.ReturnType = new CodeTypeReference (typeof (int));
            method4.Statements.Add (new CodeVariableDeclarationStatement (typeof (int), "iReturn"));

            cpr = new CodePropertyReferenceExpression (null, "System");

            cas = new CodeAssignStatement (new CodeVariableReferenceExpression ("iReturn"), cpr);
            method4.Statements.Add (cas);
            method4.Statements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("iReturn")));
            cd.Members.Add (method4);

            // Verify that what CodeTypeReferenceOptions are correctly set. 
            // Basically this check gives the code coverage for the get property
            AddScenario ("CTR_GetGlobalRefCheck", "Check that CodeTypeReference.Options gives the proper value.");
            if (ctr.Options == CodeTypeReferenceOptions.GlobalReference)
                VerifyScenario ("CTR_GetGlobalRefCheck");

            // one-off generate statements
            StringWriter sw = new StringWriter ();

            // global shouldn't be generated in this instance
            CodeTypeReference variableType = new CodeTypeReference (typeof (System.String), CodeTypeReferenceOptions.GlobalReference);
            CodeVariableDeclarationStatement variable = new CodeVariableDeclarationStatement (variableType, "myVariable");
            provider.GenerateCodeFromStatement (variable, sw, null);

            // global should be generated in this instance
            CodeTypeReference variableType2 = new CodeTypeReference (typeof (System.Array), CodeTypeReferenceOptions.GlobalReference);
            CodeVariableDeclarationStatement variable2 = new CodeVariableDeclarationStatement (variableType2, "myVariable2");
            provider.GenerateCodeFromStatement (variable2, sw, null);

            AddScenario ("GlobalKeywordShouldExist", "When an array is referred to, a global qualifier should be generated on it.");
            if (sw.ToString ().IndexOf ("global") != -1 && sw.ToString ().IndexOf ("Global") != -1) {
                LogMessage ("Global keyword does not exist in statement: " + sw.ToString ());
            }
            else
                VerifyScenario ("GlobalKeywordShouldExist");
        }
    }
开发者ID:modulexcite,项目名称:powerpack-archive,代码行数:101,代码来源:globalkeywordtest.cs


注:本文中的CodeDomProvider.GenerateCodeFromStatement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。