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();
-
-
- 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:
-
- 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:
-
- CodeSnippetExpression codeSnippet = new CodeSnippetExpression("return X + Y");
-
-
- myMethod.Statements.Add(codeSnippet);
Finally we add the method to our class definition using following code:
-
- 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);
-
-
- CodeTypeReference methodReturnType = new CodeTypeReference(typeof(System.Int32));
- CodeMemberMethod myMethod = new CodeMemberMethod();
-
-
- myMethod.Name = "MySum";
- myMethod.ReturnType = methodReturnType;
- myMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;
-
-
- CodeParameterDeclarationExpression methodPrm1 = new CodeParameterDeclarationExpression(typeof(Int32), "X");
- CodeParameterDeclarationExpression methodPrm2 = new CodeParameterDeclarationExpression(typeof(Int32), "Y");
- myMethod.Parameters.AddRange(new CodeParameterDeclarationExpression[] { methodPrm1, methodPrm2 });
-
-
- CodeSnippetExpression codeSnippet = new CodeSnippetExpression("return X + Y");
-
-
- myMethod.Statements.Add(codeSnippet);
-
-
- 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:
-
-
-
-
-
-
-
-
-
-
- namespace sampleNameSpace {
- using System;
- using System.Linq;
- using System.Text;
-
-
- public class SampleClass {
-
- public int MySum(int X, int Y) {
- return X + Y;
- }
- }
- }
So this was about how we can generate the dynamic class and add methods to it. Hope you enjoyed reading it. Happy coding...!!!