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-
- CodeTypeReference methodReturnType = new CodeTypeReference(typeof(System.Int32));
- CodeMemberMethod myMethod = new CodeMemberMethod();
-
-
- myMethod.Name = "MySum";
- myMethod.ReturnType = methodReturnType;
- 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-
-
- CodeParameterDeclarationExpression methodPrm1 = new CodeParameterDeclarationExpression(typeof(Int32), "X");
- CodeParameterDeclarationExpression methodPrm2 = new CodeParameterDeclarationExpression(typeof(Int32), "Y");
- 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-
-
- CodeSnippetExpression codeSnippet = new CodeSnippetExpression("return X + Y");
-
-
- myMethod.Statements.Add(codeSnippet);
Finally, we add the method to our class definition, using the code, given below-
-
- cls.Members.Add(myMethod);
Our complete code will look, as given below-
- CodeNamespace nameSpace = new CodeNamespace("myNameSpace");
-
- 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 = "MyClass";
- 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(@"D:\TestFile.cs", false))
- {
- IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
- provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions());
- tw.Close();
- }
Run the Application and see the code, given below-
-
-
-
-
-
-
-
-
-
-
- namespace myNameSpace {
- using System;
- using System.Linq;
- using System.Text;
-
-
- public class MyClass {
-
- public int MySum(int X, int Y) {
- return X + Y;
- }
- }
- }
This was about how we can generate the dynamic class and add methods to it. Hope, you enjoyed reading it. Happy coding...!!!