Adding Methods In Dynamic Class

In my previous article, we discussed about the concept of creating a dynamic class, using CodeDom namespace. Continuing along the same lines, we will discuss how we can add the methods to these classes. Thus, we will be using the same code, which we used in the previous discussion.
 
To add the method to the dynamic class, we will use CodeMemberMethod class, which provides different properties, which can be used to configure the definition of our methods. We will use this class to create a dynamic method, which will take two input parameters, perform their sum and return the value. Thus, we will create the basic method signatures. Hence, our code will look, as shown below-
  1. CodeTypeReference methodReturnType = new CodeTypeReference(typeof(System.Int32));
  2. CodeMemberMethod myMethod = new CodeMemberMethod();  
  3.   
  4.  // Generate Method signatures.  
  5.  myMethod.Name = "MySum";  
  6.  myMethod.ReturnType = methodReturnType;  
  7.  myMethod.Attributes = MemberAttributes.Public;  
Note the use of the CodeTypeReference class to specify the return type of our method. Next, we will use the CodeParameterDeclarationExpression class to create the parameters of our class and add them to the method. Thus, our code will look, as shown below-
  1. // Initialize for Method parameters  
  2. CodeParameterDeclarationExpression methodPrm1 = new CodeParameterDeclarationExpression(typeof(Int32), "X");  
  3. CodeParameterDeclarationExpression methodPrm2 = new CodeParameterDeclarationExpression(typeof(Int32), "Y");  
  4. myMethod.Parameters.AddRange(new CodeParameterDeclarationExpression[] { methodPrm1, methodPrm2 });  
Next, we will specify the method definition, using the CodeSnippetExpression class. Our method definition will simply return the sum of X and Y parameters. Thus, we will generate an expression to perform the sum of the two numbers and add it to the method. Thus, our code will look, as shown below-
  1. // Generate method definition  
  2. CodeSnippetExpression codeSnippet = new CodeSnippetExpression("return X + Y");  
  3.   
  4. // Add method definition to method  
  5. myMethod.Statements.Add(codeSnippet);  
Finally, we add the method to our class definition, using the code, given below-
  1. // Add method to the class.  
  2. cls.Members.Add(myMethod);  
Our complete code will look, as given below-
  1.  CodeNamespace nameSpace = new CodeNamespace("myNameSpace");  
  2.   
  3.  nameSpace.Imports.Add(new CodeNamespaceImport("System"));  
  4.  nameSpace.Imports.Add(new CodeNamespaceImport("System.Linq"));  
  5.  nameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));  
  6.   
  7.  CodeTypeDeclaration cls = new CodeTypeDeclaration();  
  8.  cls.Name = "MyClass";  
  9.  cls.IsClass = true;  
  10.  cls.Attributes = MemberAttributes.Public;  
  11.  nameSpace.Types.Add(cls);  
  12.   
  13.  // Initialize for Method Return type  
  14.  CodeTypeReference methodReturnType = new CodeTypeReference(typeof(System.Int32));  
  15.  CodeMemberMethod myMethod = new CodeMemberMethod();  
  16.   
  17.  // Generate Method signatures.  
  18.  myMethod.Name = "MySum";  
  19.  myMethod.ReturnType = methodReturnType;  
  20.  myMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;  
  21.   
  22.  // Initialize for Method parameters  
  23.  CodeParameterDeclarationExpression methodPrm1 = new CodeParameterDeclarationExpression(typeof(Int32), "X");  
  24.  CodeParameterDeclarationExpression methodPrm2 = new CodeParameterDeclarationExpression(typeof(Int32), "Y");  
  25.  myMethod.Parameters.AddRange(new CodeParameterDeclarationExpression[] { methodPrm1, methodPrm2 });  
  26.   
  27.  // Generate method definition  
  28.  CodeSnippetExpression codeSnippet = new       CodeSnippetExpression("return X + Y");  
  29.   
  30. // Add method definition to method  
  31.  myMethod.Statements.Add(codeSnippet);  
  32.   
  33.  // Add method to the class.  
  34.  cls.Members.Add(myMethod);  
  35.   
  36.  CodeCompileUnit compileUnit = new CodeCompileUnit();  
  37.  compileUnit.Namespaces.Add(nameSpace);  
  38.  CSharpCodeProvider csharpcodeprovider = new CSharpCodeProvider();  
  39.   
  40.  CSharpCodeProvider provider = new CSharpCodeProvider();  
  41.   
  42.  using (StreamWriter sw = new StreamWriter(@"D:\TestFile.cs"false))  
  43.  {  
  44.      IndentedTextWriter tw = new IndentedTextWriter(sw, "    ");  
  45.      provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions());  
  46.      tw.Close();  
  47.  }  
Run the Application and see the code, given below-
  1. //------------------------------------------------------------------------------  
  2. // <auto-generated>  
  3. //     This code was generated by a tool.  
  4. //     Runtime Version:4.0.30319.42000  
  5. //  
  6. //     Changes to this file may cause incorrect behavior and will be lost if  
  7. //     the code is regenerated.  
  8. // </auto-generated>  
  9. //------------------------------------------------------------------------------  
  10.   
  11. namespace myNameSpace {  
  12.     using System;  
  13.     using System.Linq;  
  14.     using System.Text;  
  15.       
  16.       
  17.     public class MyClass {  
  18.           
  19.         public int MySum(int X, int Y) {  
  20.             return X + Y;  
  21.         }  
  22.     }  
  23. }  
This was about how we can generate the dynamic class and add methods to it. Hope, you enjoyed reading it. Happy coding...!!!


Recommended Free Ebook
Similar Articles