How to create a method with parameters?

I want to create a method object of a class with the class CodeMemberMethod,

This method may need serveral parameters, the parameters may be simple data types like Int, bool, or may be objects of some classes like DataTable.

How to create a method with parameters




Answer this question

How to create a method with parameters?

  • shadownnc

    I'm not sure how to answer this. I'm guessing you've found some wizard to add methods I'm sure such a wizard exists, however in many years of programming ( longer than .NET has been around, that's for sure ), I've only briefly used such a wizard when I started with ATL ( where I needed to add code in three places, and it was new to me ). You add methods by typed them in to your IDE. You can put any parameters you like in there. The less you rely on wizards, the more likely it is that you're a progammer :-)



  • sboots

    Chris, thank you very much for your really helpful suggestion~!

    The parameters for the method are given dynamically, the following snippet is my idea of setting parameters

    protected CodeMemberMethod CreateMethodWithParameters(..., DataTable dtParaTable)

    {

    ...

    foreach(DataRow drPara in dtParaTable.Rows)
    {
    objMethod.Parameters.Add(new CodeParameterDeclarationExpression(dr["Type"].ToString(),dr["Name"].ToString()));
    }

    ...

    }

    Do you have any better opinions THX.



  • Jerrycee11

    Oh, my apologies. *blush*



  • Jaqq

    Thank you, cgraus,

    I guess maybe I didn't express myself clear.

    It is My program that generates some C# codes automatically,

    it uses the class System.CodeDom.CodeMemberMethod to describe a method.

    I want to know How to create a method with parameters dynamically in the automatically generated code.



  • zatom idham

    Hi,



    // Create an instance
    CodeMemberMethod m = new CodeMemberMethod();

    // Set the name of the member method
    m.Name = "MyFunction";

    // If there is a return type, specify it
    m.ReturnType = new CodeTypeReference("System.String");

    // Add the parameters
    m.Parameters.Add(
      new CodeParameterDeclarationExpression(
         "System.Int32",      // Parameter type
         "MyParam"            // Parameter name
      )
    );

    // Finally, add the method's statements
    m.Statements.Add(
       new CodeMethodReturnStatement(
            new CodeArgumentReferenceExpression ("blah") )
    );

     


    The above code will generate:



    string MyFunction(int MyParam)
    {
       return "blah";
    }

     


    Hope this helps,

    -chris



  • Cortex

    You can repeat this line:



    // Add the parameters
    m.Parameters.Add(
    new CodeParameterDeclarationExpression(
    "System.Int32", // Parameter type
    "MyParam" // Parameter name
    )
    );




    For as many dynamic parameters there is. Set the type and name for each parameter declarations.

    Regards,

    -chris

  • How to create a method with parameters?