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


C# CodeMemberMethod类代码示例

本文整理汇总了C#中CodeMemberMethod的典型用法代码示例。如果您正苦于以下问题:C# CodeMemberMethod类的具体用法?C# CodeMemberMethod怎么用?C# CodeMemberMethod使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: BuildTree

    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu) {
        //cu.UserData["AllowLateBound"] = true;

        // GENERATES (C#):
        //  namespace Namespace1 {
        //      using System;
        //      
        //      
        //      public class Class1 {
        //          
        //          public virtual string Foo1(string format, [System.Runtime.InteropServices.OptionalAttribute()] params object[] array) {
        //              string str;
        //              str = format.Replace("{0}", array[0].ToString());
        //              str = str.Replace("{1}", array[1].ToString());
        //              str = str.Replace("{2}", array[2].ToString());
        //              return str;
        //          }
        //      }
        //  }

        CodeNamespace ns = new CodeNamespace("Namespace1");
        ns.Imports.Add(new CodeNamespaceImport("System"));
        cu.Namespaces.Add(ns);

        // Full Verification Objects
        CodeTypeDeclaration class1 = new CodeTypeDeclaration();
        class1.Name = "Class1";

        ns.Types.Add(class1);

        AddScenario ("CheckFoo1");
        CodeMemberMethod fooMethod1 = new CodeMemberMethod();
        fooMethod1.Name = "Foo1";
        fooMethod1.Attributes = MemberAttributes.Public ; 
        fooMethod1.ReturnType = new CodeTypeReference(typeof(string));

        CodeParameterDeclarationExpression parameter1 = new CodeParameterDeclarationExpression();
        parameter1.Name = "format";
        parameter1.Type = new CodeTypeReference(typeof(string));
        fooMethod1.Parameters.Add(parameter1);

        CodeParameterDeclarationExpression parameter2 = new CodeParameterDeclarationExpression();
        parameter2.Name = "array";
        parameter2.Type = new CodeTypeReference(typeof(object[]));

        if (Supports (provider, GeneratorSupport.ParameterAttributes)) {
            parameter2.CustomAttributes.Add( new CodeAttributeDeclaration("System.ParamArrayAttribute"));
            parameter2.CustomAttributes.Add( new CodeAttributeDeclaration("System.Runtime.InteropServices.OptionalAttribute"));
        }
        fooMethod1.Parameters.Add(parameter2);
        class1.Members.Add(fooMethod1);
        
        fooMethod1.Statements.Add( new CodeVariableDeclarationStatement(typeof(string), "str")); 
            
        fooMethod1.Statements.Add(CreateStatement(new CodeArgumentReferenceExpression ("format"), 0));
        fooMethod1.Statements.Add(CreateStatement(new CodeVariableReferenceExpression ("str"), 1));
        fooMethod1.Statements.Add(CreateStatement(new CodeVariableReferenceExpression ("str"), 2));
       
        fooMethod1.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("str")));
    }
开发者ID:drvink,项目名称:FSharp.Compiler.CodeDom,代码行数:60,代码来源:paramstest.cs

示例2: BuildTree

    public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {

        // GENERATES (C#):
        //
        //  namespace NSPC {
        //      
        //      public class ClassWithMethod {
        //          
        //          public int MethodName() {
        //              This is a CODE SNIPPET #*$*@;
        //              return 3;
        //          }
        //      }
        //  }
        AddScenario ("FindSnippet", "Find code snippet in the code.");
        CodeNamespace nspace = new CodeNamespace ("NSPC");
        cu.Namespaces.Add (nspace);

        CodeTypeDeclaration class1 = new CodeTypeDeclaration ("ClassWithMethod");
        class1.IsClass = true;
        nspace.Types.Add (class1);

        CodeMemberMethod cmm = new CodeMemberMethod ();
        cmm.Name = "MethodName";
        cmm.Attributes = MemberAttributes.Public | MemberAttributes.Final;
        cmm.ReturnType = new CodeTypeReference (typeof (int));
        cmm.Statements.Add (new CodeExpressionStatement (new CodeSnippetExpression ("This is a CODE SNIPPET #*$*@")));
        cmm.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (3)));
        class1.Members.Add (cmm);
    }
开发者ID:modulexcite,项目名称:powerpack-archive,代码行数:30,代码来源:codesnippettest.cs

示例3: BuildTree

    public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
        CodeNamespace ns = new CodeNamespace ("Namespace1");
        cu.Namespaces.Add (ns);

        // GENERATES (C#):
        //
        //   namespace Namespace1 {
        //      public class Class1 {
        //          public int Method1 {
        //              #line 300 "LinedStatement"
        //              return 0;
        //
        //              #line default
        //              #line hidden
        //          }
        //      }
        //   }

        CodeTypeDeclaration class1 = new CodeTypeDeclaration ("Class1");
        class1.IsClass = true;
        class1.Attributes = MemberAttributes.Public;
        ns.Types.Add (class1);

        CodeMemberMethod method1 = new CodeMemberMethod ();
        method1.ReturnType = new CodeTypeReference (typeof (int));
        method1.Name = "Method1";
        class1.Members.Add (method1);

        AddScenario ("FindLinedStatement");
        CodeMethodReturnStatement ret = new CodeMethodReturnStatement (new CodePrimitiveExpression (0));
        ret.LinePragma = new CodeLinePragma ("LinedStatement", 300);
        method1.Statements.Add (ret);
    }
开发者ID:drvink,项目名称:FSharp.Compiler.CodeDom,代码行数:33,代码来源:linepragmatest.cs

示例4: ParametersEqual

 public static bool ParametersEqual(this CodeMemberMethod self, CodeMemberMethod method)
 {
     if (method.Parameters.Count != self.Parameters.Count)
         return false;
     for (int i = 0; i < self.Parameters.Count; i++) {
         if (!self.Parameters[i].Type.TypeEquals(method.Parameters[i].Type) || self.Parameters[i].Direction != method.Parameters[i].Direction) {
             return false;
         }
     }
     return true;
 }
开发者ID:KDE,项目名称:assemblygen,代码行数:11,代码来源:CodeDomExtensions.cs

示例5: OperationContractGenerationContext

 public OperationContractGenerationContext(ServiceContractGenerator serviceContractGenerator, ServiceContractGenerationContext contract, 
    OperationDescription operation, CodeTypeDeclaration declaringType, CodeMemberMethod method) : this(serviceContractGenerator, contract, operation, declaringType)
 {
     if (method == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("method"));
     }
     this.syncMethod = method;
     this.beginMethod = null;
     this.endMethod = null;
 }
开发者ID:gtri-iead,项目名称:LEXS-NET-Sample-Implementation-3.1.4,代码行数:11,代码来源:OperationContractGenerationContext.cs

示例6: ItemMethod

    public ItemMethod(string inName, MemberAttributes inAtt, List<string> inParameters)
    {
        method = new CodeMemberMethod();
        method.Name = inName;
        method.Attributes = inAtt;
        for (int i = 0; i < inParameters.Count; i++)
        {

            method.Parameters.Add(new CodeParameterDeclarationExpression(inParameters[i], "inArg" + i));
        }
        statement = new CodeConditionStatement();
    }
开发者ID:killliu,项目名称:AutoCSharp,代码行数:12,代码来源:ItemMethod.cs

示例7: BuildTree

    public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {

        // GENERATES (C#):
        //
        //  public class MyConverter : System.ComponentModel.TypeConverter {
        //      
        //      private void Foo() {
        //          this.Foo(null);
        //      }
        //      
        //      private void Foo(string s) {
        //      }
        //      
        //      public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) {
        //          return base.CanConvertFrom(context, sourceType);
        //      }
        //  }

        CodeNamespace ns = new CodeNamespace ();
        cu.Namespaces.Add (ns);

        CodeTypeDeclaration class1 = new CodeTypeDeclaration ();
        class1.Name = "MyConverter";
        class1.BaseTypes.Add (new CodeTypeReference (typeof (System.ComponentModel.TypeConverter)));
        ns.Types.Add (class1);

        CodeMemberMethod foo1 = new CodeMemberMethod ();
        foo1.Name = "Foo";
        foo1.Statements.Add (new CodeMethodInvokeExpression (new CodeThisReferenceExpression (), "Foo", new CodePrimitiveExpression (null)));
        class1.Members.Add (foo1);

        CodeMemberMethod foo2 = new CodeMemberMethod ();
        foo2.Name = "Foo";
        foo2.Parameters.Add (new CodeParameterDeclarationExpression (typeof (string), "s"));
        class1.Members.Add (foo2);

        CodeMemberMethod convert = new CodeMemberMethod ();
        convert.Name = "CanConvertFrom";
        convert.Attributes = MemberAttributes.Public | MemberAttributes.Override | MemberAttributes.Overloaded;
        convert.ReturnType = new CodeTypeReference (typeof (bool));
        convert.Parameters.Add (new CodeParameterDeclarationExpression (typeof (System.ComponentModel.ITypeDescriptorContext), "context"));
        convert.Parameters.Add (new CodeParameterDeclarationExpression (typeof (System.Type), "sourceType"));
        convert.Statements.Add (
            new CodeMethodReturnStatement (
            new CodeMethodInvokeExpression (
            new CodeBaseReferenceExpression (),
            "CanConvertFrom",
            new CodeArgumentReferenceExpression ("context"),
            new CodeArgumentReferenceExpression ("sourceType"))));
        class1.Members.Add (convert);
    }
开发者ID:drvink,项目名称:FSharp.Compiler.CodeDom,代码行数:51,代码来源:overloadtest.cs

示例8: Main

	static void Main(string[] args)
	{
		int namespaces = 10;
		int classesPerNamespace = 10;
		int methodsPerClass = 10;

		CodeCompileUnit codeCompileUnit = new CodeCompileUnit();

		for (int i = 0; i < namespaces; ++i)
		{
			CodeNamespace codeNamespace = new CodeNamespace();
			codeCompileUnit.Namespaces.Add(codeNamespace);
			codeNamespace.Name = "Namespace" + i;

			for (int j = 0; j < classesPerNamespace; ++j)
			{
				CodeTypeDeclaration codeTypeDeclaration = new CodeTypeDeclaration();
				codeNamespace.Types.Add(codeTypeDeclaration);
				codeTypeDeclaration.Name = "Class" + j;
				codeTypeDeclaration.TypeAttributes = TypeAttributes.Public;
				
				codeTypeDeclaration.Comments.Add(
					new CodeCommentStatement(
						"<summary>This is a summary.</summary>",
						true
					)
				);

				for (int k = 0; k < methodsPerClass; ++k)
				{
					CodeMemberMethod codeMemberMethod = new CodeMemberMethod();
					codeTypeDeclaration.Members.Add(codeMemberMethod);
					codeMemberMethod.Name = "Method" + k;
					codeMemberMethod.Attributes = MemberAttributes.Public;

					codeMemberMethod.Comments.Add(
						new CodeCommentStatement(
							"<summary>This is a summary.</summary>",
							true
						)
					);
				}
			}
		}

		CodeDomProvider codeDomProvider = new CSharpCodeProvider();
		ICodeGenerator codeGenerator = codeDomProvider.CreateGenerator();
		CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions();
		codeGenerator.GenerateCodeFromCompileUnit(codeCompileUnit, Console.Out, codeGeneratorOptions);
	}
开发者ID:tgassner,项目名称:NDoc,代码行数:50,代码来源:NDocGen.cs

示例9: BuildTree

    public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
        CodeNamespace ns = new CodeNamespace ();
        ns.Name = "MyNamespace";
        cu.Namespaces.Add (ns);

        // GENERATES (C#):
        //
        // namespace MyNamespace {
        //     public class MyClass {
        //         private void PrimitiveTest() {
        //             char var1 = 'a';
        //             char var2 = '\0';
        //             string var3 = "foo\0bar\0baz\0";
        //             object var4 = null;
        //             int var5 = 42;
        //             double var6 = 3.14;
        //             System.Console.Write(var1);
        //             System.Console.Write(var2);
        //             System.Console.Write(var3);
        //             System.Console.Write(var4);
        //             System.Console.Write(var5);
        //             System.Console.Write(var6);
        //         }
        //     }
        // }
        
        CodeTypeDeclaration class1 = new CodeTypeDeclaration ();
        class1.Name = "MyClass";
        class1.IsClass = true;
        class1.Attributes = MemberAttributes.Public;
        ns.Types.Add (class1);


        CodeMemberMethod method = new CodeMemberMethod ();
        method.Name = "PrimitiveTest";
        method.Statements.Add (new CodeVariableDeclarationStatement (typeof (char), "var1", new CodePrimitiveExpression ('a')));
        method.Statements.Add (new CodeVariableDeclarationStatement (typeof (char), "var2", new CodePrimitiveExpression ('\0')));
        method.Statements.Add (new CodeVariableDeclarationStatement (typeof (string), "var3", new CodePrimitiveExpression ("foo\0bar\0baz\0")));
        method.Statements.Add (new CodeVariableDeclarationStatement (typeof (Object), "var4", new CodePrimitiveExpression (null)));
        method.Statements.Add (new CodeVariableDeclarationStatement (typeof (int), "var5", new CodePrimitiveExpression (42)));
        method.Statements.Add (new CodeVariableDeclarationStatement (typeof (double), "var6", new CodePrimitiveExpression (3.14)));
        method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var1")));
        method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var2")));
        method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var3")));
        method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var4")));
        method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var5")));
        method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var6")));
        class1.Members.Add (method);
    }
开发者ID:drvink,项目名称:FSharp.Compiler.CodeDom,代码行数:49,代码来源:codeprimitiveexpressiontest.cs

示例10: AddMethodImpl

 private static void AddMethodImpl(CodeMemberMethod method)
 {
     CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(GetChannelReference(), method.Name, new CodeExpression[0]);
     foreach (CodeParameterDeclarationExpression expression2 in method.Parameters)
     {
         expression.Parameters.Add(new CodeDirectionExpression(expression2.Direction, new CodeVariableReferenceExpression(expression2.Name)));
     }
     if (IsVoid(method))
     {
         method.Statements.Add(expression);
     }
     else
     {
         method.Statements.Add(new CodeMethodReturnStatement(expression));
     }
 }
开发者ID:gtri-iead,项目名称:LEXS-NET-Sample-Implementation-3.1.4,代码行数:16,代码来源:ClientClassGenerator.cs

示例11: GenerateCode

 private static string GenerateCode(string expression)
 {
     var source = new StringBuilder();
     var sw = new StringWriter(source);
     var options = new CodeGeneratorOptions();
     var myNamespace = new CodeNamespace("ExpressionEvaluator");
     myNamespace.Imports.Add(new CodeNamespaceImport("System"));
     var classDeclaration = new CodeTypeDeclaration { IsClass = true, Name = "Calculator", Attributes = MemberAttributes.Public };
     var myMethod = new CodeMemberMethod { Name = "Calculate", ReturnType = new CodeTypeReference(typeof(double)), Attributes = MemberAttributes.Public };
     myMethod.Statements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression(expression)));
     classDeclaration.Members.Add(myMethod);
     myNamespace.Types.Add(classDeclaration);
     provider.GenerateCodeFromNamespace(myNamespace, sw, options);
     sw.Flush();
     sw.Close();
     return source.ToString();
 }
开发者ID:ATouhou,项目名称:QuranCode,代码行数:17,代码来源:Calculator.cs

示例12: HasMethod

    public static bool HasMethod(this CodeTypeDeclaration self, CodeMemberMethod method)
    {
        foreach (CodeTypeMember member in self.Members) {
            if (!(member is CodeMemberMethod) || member.Name != method.Name)
                continue;

            // now check the parameters
            CodeMemberMethod currentMeth = (CodeMemberMethod) member;
            if (currentMeth.Parameters.Count != method.Parameters.Count)
                continue;
            bool continueOuter = false;
            for (int i = 0; i < method.Parameters.Count; i++) {
                if (!method.Parameters[i].Type.TypeEquals(currentMeth.Parameters[i].Type) || method.Parameters[i].Direction != currentMeth.Parameters[i].Direction) {
                    continueOuter = true;
                    break;
                }
            }
            if (continueOuter)
                continue;
            return true;
        }
        return false;
    }
开发者ID:KDE,项目名称:assemblygen,代码行数:23,代码来源:CodeDomExtensions.cs

示例13: Arrays_SingleDimensional_PrimitiveTypes

        public void Arrays_SingleDimensional_PrimitiveTypes()
        {
            var arrayMethod = new CodeMemberMethod();
            arrayMethod.Name = "ArrayMethod";
            arrayMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "parameter"));
            arrayMethod.Attributes = (arrayMethod.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            arrayMethod.ReturnType = new CodeTypeReference(typeof(System.Int64));
            arrayMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "arraySize", new CodePrimitiveExpression(3)));
            arrayMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(int[]), "array1"));
            arrayMethod.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("System.Int32", 1), "array2", new CodeArrayCreateExpression(typeof(int[]), new CodePrimitiveExpression(3))));
            arrayMethod.Statements.Add(
                new CodeVariableDeclarationStatement(new CodeTypeReference("System.Int16", 1), "array3", new CodeArrayCreateExpression(new CodeTypeReference("System.Int16", 1),
                new CodeExpression[] {
                                         new CodePrimitiveExpression(1),
                                         new CodePrimitiveExpression(4),
                                         new CodePrimitiveExpression(9),})));
            arrayMethod.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("array1"), new CodeArrayCreateExpression(typeof(int[]), new CodeVariableReferenceExpression("arraySize"))));
            arrayMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(System.Int64), "retValue", new CodePrimitiveExpression(0)));
            arrayMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "i"));
            arrayMethod.Statements.Add(
                new CodeIterationStatement(
                new CodeAssignStatement(new CodeVariableReferenceExpression("i"), new CodePrimitiveExpression(0)),
                new CodeBinaryOperatorExpression(
                new CodeVariableReferenceExpression("i"),
                CodeBinaryOperatorType.LessThan,
                new CodePropertyReferenceExpression(
                new CodeVariableReferenceExpression("array1"), "Length")),
                new CodeAssignStatement(
                new CodeVariableReferenceExpression("i"),
                new CodeBinaryOperatorExpression(
                new CodeVariableReferenceExpression("i"),
                CodeBinaryOperatorType.Add,
                new CodePrimitiveExpression(1))),
                new CodeAssignStatement(
                new CodeArrayIndexerExpression(
                new CodeVariableReferenceExpression("array1"),
                new CodeVariableReferenceExpression("i")),
                new CodeBinaryOperatorExpression(
                new CodeVariableReferenceExpression("i"),
                CodeBinaryOperatorType.Multiply,
                new CodeVariableReferenceExpression("i"))),
                new CodeAssignStatement(
                new CodeArrayIndexerExpression(
                new CodeVariableReferenceExpression("array2"),
                new CodeVariableReferenceExpression("i")),
                new CodeBinaryOperatorExpression(
                new CodeArrayIndexerExpression(
                new CodeVariableReferenceExpression("array1"),
                new CodeVariableReferenceExpression("i")),
                CodeBinaryOperatorType.Subtract,
                new CodeVariableReferenceExpression("i"))),
                new CodeAssignStatement(
                new CodeVariableReferenceExpression("retValue"),
                new CodeBinaryOperatorExpression(
                new CodeVariableReferenceExpression("retValue"),
                CodeBinaryOperatorType.Add,
                new CodeBinaryOperatorExpression(
                new CodeArrayIndexerExpression(
                new CodeVariableReferenceExpression("array1"),
                new CodeVariableReferenceExpression("i")),
                CodeBinaryOperatorType.Add,
                new CodeBinaryOperatorExpression(
                new CodeArrayIndexerExpression(
                new CodeVariableReferenceExpression("array2"),
                new CodeVariableReferenceExpression("i")),
                CodeBinaryOperatorType.Add,
                new CodeArrayIndexerExpression(
                new CodeVariableReferenceExpression("array3"),
                new CodeVariableReferenceExpression("i"))))))));
            arrayMethod.Statements.Add(
                new CodeMethodReturnStatement(new CodeVariableReferenceExpression("retValue")));

            AssertEqual(arrayMethod,
                @"Public Function ArrayMethod(ByVal parameter As Integer) As Long
                      Dim arraySize As Integer = 3
                      Dim array1() As Integer
                      Dim array2((3) - 1) As Integer
                      Dim array3() As Short = New Short() {1, 4, 9}
                      array1 = New Integer((arraySize) - 1) {}
                      Dim retValue As Long = 0
                      Dim i As Integer
                      i = 0
                      Do While (i < array1.Length)
                          array1(i) = (i * i)
                          array2(i) = (array1(i) - i)
                          retValue = (retValue  _
                                      + (array1(i)  _
                                      + (array2(i) + array3(i))))
                          i = (i + 1)
                      Loop
                      Return retValue
                  End Function");
        }
开发者ID:geoffkizer,项目名称:corefx,代码行数:93,代码来源:VBCodeGenerationTests.cs

示例14: MethodWithRefParameter

        public void MethodWithRefParameter()
        {
            CodeTypeDeclaration cd = new CodeTypeDeclaration("TEST");
            cd.IsClass = true;

            CodeMemberMethod cmm = new CodeMemberMethod();
            cmm.Name = "Work";
            cmm.ReturnType = new CodeTypeReference("System.void");
            cmm.Attributes = MemberAttributes.Static;
            // add parameter with ref direction
            CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(typeof(int), "i");
            param.Direction = FieldDirection.Ref;
            cmm.Parameters.Add(param);
            // add parameter with out direction
            param = new CodeParameterDeclarationExpression(typeof(int), "j");
            param.Direction = FieldDirection.Out;
            cmm.Parameters.Add(param);
            cmm.Statements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("i"),
                new CodeBinaryOperatorExpression(new CodeArgumentReferenceExpression("i"),
                CodeBinaryOperatorType.Add, new CodePrimitiveExpression(4))));
            cmm.Statements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("j"),
                new CodePrimitiveExpression(5)));
            cd.Members.Add(cmm);

            cmm = new CodeMemberMethod();
            cmm.Name = "CallingWork";
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression parames = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(parames);
            cmm.ReturnType = new CodeTypeReference("System.int32");
            cmm.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
                new CodePrimitiveExpression(10)));
            cmm.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "b"));
            // invoke the method called "work"
            CodeMethodInvokeExpression methodinvoked = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression
                (new CodeTypeReferenceExpression("TEST"), "Work"));
            // add parameter with ref direction
            CodeDirectionExpression parameter = new CodeDirectionExpression(FieldDirection.Ref,
                new CodeVariableReferenceExpression("a"));
            methodinvoked.Parameters.Add(parameter);
            // add parameter with out direction
            parameter = new CodeDirectionExpression(FieldDirection.Out, new CodeVariableReferenceExpression("b"));
            methodinvoked.Parameters.Add(parameter);
            cmm.Statements.Add(methodinvoked);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeBinaryOperatorExpression
                (new CodeVariableReferenceExpression("a"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("b"))));
            cd.Members.Add(cmm);

            AssertEqual(cd,
                @"Public Class TEST
                  Shared Sub Work(ByRef i As Integer, ByRef j As Integer)
                      i = (i + 4)
                      j = 5
                  End Sub
                  Public Shared Function CallingWork(ByVal a As Integer) As Integer
                      a = 10
                      Dim b As Integer
                      TEST.Work(a, b)
                      Return (a + b)
                  End Function
              End Class");
        }
开发者ID:geoffkizer,项目名称:corefx,代码行数:62,代码来源:VBCodeGenerationTests.cs

示例15: GenericTypesAndConstraints

        public void GenericTypesAndConstraints()
        {
            CodeNamespace ns = new CodeNamespace("NS");
            ns.Imports.Add(new CodeNamespaceImport("System"));
            ns.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));

            CodeTypeDeclaration class1 = new CodeTypeDeclaration();
            class1.Name = "MyDictionary";
            class1.BaseTypes.Add(new CodeTypeReference("Dictionary", new CodeTypeReference[] { new CodeTypeReference("TKey"), new CodeTypeReference("TValue"), }));
            CodeTypeParameter kType = new CodeTypeParameter("TKey");
            kType.HasConstructorConstraint = true;
            kType.Constraints.Add(new CodeTypeReference(typeof(IComparable)));
            kType.CustomAttributes.Add(new CodeAttributeDeclaration(
                "System.ComponentModel.DescriptionAttribute", new CodeAttributeArgument(new CodePrimitiveExpression("KeyType"))));

            CodeTypeReference iComparableT = new CodeTypeReference("IComparable");
            iComparableT.TypeArguments.Add(new CodeTypeReference(kType));
            kType.Constraints.Add(iComparableT);

            CodeTypeParameter vType = new CodeTypeParameter("TValue");
            vType.Constraints.Add(new CodeTypeReference(typeof(IList<System.String>)));
            vType.CustomAttributes.Add(new CodeAttributeDeclaration(
                "System.ComponentModel.DescriptionAttribute", new CodeAttributeArgument(new CodePrimitiveExpression("ValueType"))));

            class1.TypeParameters.Add(kType);
            class1.TypeParameters.Add(vType);
            ns.Types.Add(class1);

            // Declare a generic method.
            CodeMemberMethod printMethod = new CodeMemberMethod();
            CodeTypeParameter sType = new CodeTypeParameter("S");
            sType.HasConstructorConstraint = true;
            CodeTypeParameter tType = new CodeTypeParameter("T");
            sType.HasConstructorConstraint = true;

            printMethod.Name = "Nop";
            printMethod.TypeParameters.Add(sType);
            printMethod.TypeParameters.Add(tType);
            printMethod.Attributes = MemberAttributes.Public;
            class1.Members.Add(printMethod);

            var class2 = new CodeTypeDeclaration();
            class2.Name = "Demo";

            var methodMain = new CodeEntryPointMethod();
            var myClass = new CodeTypeReference(
                "MyDictionary",
                new CodeTypeReference[] {
                    new CodeTypeReference(typeof(int)),
                    new CodeTypeReference("List",
                       new CodeTypeReference[]
                            {new CodeTypeReference("System.String") })});
            methodMain.Statements.Add(new CodeVariableDeclarationStatement(myClass, "dict", new CodeObjectCreateExpression(myClass)));
            string dictionaryTypeName = typeof(System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<string>>[]).FullName;

            var dictionaryType = new CodeTypeReference(dictionaryTypeName);
            methodMain.Statements.Add(
                  new CodeVariableDeclarationStatement(dictionaryType, "dict2",
                     new CodeArrayCreateExpression(dictionaryType, new CodeExpression[1] { new CodePrimitiveExpression(null) })));

            class2.Members.Add(methodMain);
            ns.Types.Add(class2);

            AssertEqual(ns,
                @"Imports System
                  Imports System.Collections.Generic
                  Namespace NS
                      Public Class MyDictionary(Of TKey As  {System.IComparable, IComparable(Of TKey), New}, TValue As System.Collections.Generic.IList(Of String))
                          Inherits Dictionary(Of TKey, TValue)
                          Public Overridable Sub Nop(Of S As New, T)()
                          End Sub
                      End Class
                      Public Class Demo
                          Public Shared Sub Main()
                              Dim dict As MyDictionary(Of Integer, List(Of String)) = New MyDictionary(Of Integer, List(Of String))()
                              Dim dict2() As System.Collections.Generic.Dictionary(Of Integer, System.Collections.Generic.List(Of String)) = New System.Collections.Generic.Dictionary(Of Integer, System.Collections.Generic.List(Of String))() {Nothing}
                          End Sub
                      End Class
                  End Namespace");
        }
开发者ID:geoffkizer,项目名称:corefx,代码行数:80,代码来源:VBCodeGenerationTests.cs


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