public void Params()
{
Func<string, int, CodeStatement> createStatement = (objName, iNum) =>
{
CodeAssignStatement statement = new CodeAssignStatement(new CodeVariableReferenceExpression("str"),
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression(new CodeTypeReference(objName)), "Replace"),
new CodeExpression[]{
new CodePrimitiveExpression("{" + iNum + "}"),
new CodeMethodInvokeExpression(
new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("array"), new CodePrimitiveExpression(iNum)),
"ToString")}));
return statement;
};
CodeNamespace ns = new CodeNamespace("Namespace1");
ns.Imports.Add(new CodeNamespaceImport("System"));
CodeTypeDeclaration class1 = new CodeTypeDeclaration();
class1.Name = "Class1";
ns.Types.Add(class1);
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[]));
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("format", 0));
fooMethod1.Statements.Add(createStatement("str", 1));
fooMethod1.Statements.Add(createStatement("str", 2));
fooMethod1.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("str")));
CodeEntryPointMethod methodMain = new CodeEntryPointMethod();
methodMain.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("Class1"), "test1", new CodeObjectCreateExpression(new CodeTypeReference("Class1"))));
methodMain.Statements.Add(new CodeExpressionStatement(
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference("test1")), "Foo1"),
new CodeExpression[] {
new CodePrimitiveExpression("{0} + {1} = {2}"),
new CodePrimitiveExpression(1),
new CodePrimitiveExpression(2),
new CodePrimitiveExpression(3)
})));
class1.Members.Add(methodMain);
AssertEqual(ns,
@"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;
}
public static void Main() {
Class1 test1 = new Class1();
test1.Foo1(""{0} + {1} = {2}"", 1, 2, 3);
}
}
}");
}