In my previous article, we discussed how we can create a dynamic class using CodeDom namespace. Now we will discuss how we can add methods to these classes. For this we will be use the same code as in our previous discussion.
For adding method to the class, we will use CodeMemberMethod class, which provides different properties, to define methods. For our sample we will create a dynamic method which will take two input parameters, calculate their sum and return the sum. Let's with basic method signatures. So our code will look like the following:
- CodeTypeReference methodReturnType = new CodeTypeReference(typeof(System.Int32));
- CodeMemberMethod myMethod = new CodeMemberMethod();
- // Generate Method signatures.
- myMethod.Name = "MySum";
- myMethod.ReturnType = methodReturnType;
- myMethod.Attributes = MemberAttributes.Public;
We use of the CodeTypeReference class to specify the return type of our method. To add parameters for this method, we will use the CodeParameterDeclarationExpression class. So our code will look like the following:
- // Initialize for Method parameters
- CodeParameterDeclarationExpression methodPrm1 = new CodeParameterDeclarationExpression(typeof(Int32), "X");
- CodeParameterDeclarationExpression methodPrm2 = new CodeParameterDeclarationExpression(typeof(Int32), "Y");
- myMethod.Parameters.AddRange(new CodeParameterDeclarationExpression[] { methodPrm1, methodPrm2 });
Next, to specify the method definition we use the CodeSnippetExpression class. Our method will simply the sum of our input parameters. So we will generate an expression to perform the sum of the numbers and add it to the method. So our code will change to the following:
- // Generate method definition
- CodeSnippetExpression codeSnippet = new CodeSnippetExpression("return X + Y");
- // Add method definition to method
- myMethod.Statements.Add(codeSnippet);
- // Add method to the class.
- cls.Members.Add(myMethod);
So our complete code will look like the following:
- CodeNamespace nameSpace = new CodeNamespace("sampleNameSpace");
- nameSpace.Imports.Add(new CodeNamespaceImport("System"));
- nameSpace.Imports.Add(new CodeNamespaceImport("System.Linq"));
- nameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));
- CodeTypeDeclaration cls = new CodeTypeDeclaration();
- cls.Name = "SampleClass";
- cls.IsClass = true;
- cls.Attributes = MemberAttributes.Public;
- nameSpace.Types.Add(cls);
- // Initialize for Method Return type
- CodeTypeReference methodReturnType = new CodeTypeReference(typeof(System.Int32));
- CodeMemberMethod myMethod = new CodeMemberMethod();
- // Generate Method signatures.
- myMethod.Name = "MySum";
- myMethod.ReturnType = methodReturnType;
- myMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;
- // Initialize for Method parameters
- CodeParameterDeclarationExpression methodPrm1 = new CodeParameterDeclarationExpression(typeof(Int32), "X");
- CodeParameterDeclarationExpression methodPrm2 = new CodeParameterDeclarationExpression(typeof(Int32), "Y");
- myMethod.Parameters.AddRange(new CodeParameterDeclarationExpression[] { methodPrm1, methodPrm2 });
- // Generate method definition
- CodeSnippetExpression codeSnippet = new CodeSnippetExpression("return X + Y");
- // Add method definition to method
- myMethod.Statements.Add(codeSnippet);
- // Add method to the class.
- cls.Members.Add(myMethod);
- CodeCompileUnit compileUnit = new CodeCompileUnit();
- compileUnit.Namespaces.Add(nameSpace);
- CSharpCodeProvider csharpcodeprovider = new CSharpCodeProvider();
- CSharpCodeProvider provider = new CSharpCodeProvider();
- using (StreamWriter sw = new StreamWriter(@"C:\TestFile.cs", false))
- {
- IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
- provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions());
- tw.Close();
- }
Run the application and see the code getting generated:
- //------------------------------------------------------------------------------
- // <auto-generated>
- // This code was generated by a tool.
- // Runtime Version:4.0.30319.42000
- //
- // Changes to this file may cause incorrect behavior and will be lost if
- // the code is regenerated.
- // </auto-generated>
- //------------------------------------------------------------------------------
- namespace sampleNameSpace {
- using System;
- using System.Linq;
- using System.Text;
- public class SampleClass {
- public int MySum(int X, int Y) {
- return X + Y;
- }
- }
- }
No comments:
Post a Comment