[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Writing source code to a file

How could I go about adding a method to a sourcecode file that already exists?  i.e. add a method to a class?
2 Solutions
Thandava VallepalliCommented:

here is the link to conver vb.net code to c# code.


using System;
using System.Collections;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
public class ParseExpr

 public static void Main()
   Console.WriteLine("Expression evaluation demo");
   Console.WriteLine("Type an arithmetic expression: ");
   string Expression = Console.ReadLine();
   Console.WriteLine("Your expression is evaluated as: ");

 private static float ProcessExpression(string Expression)
   VBCodeProvider VBcp = new VBCodeProvider();
   ICodeGenerator cg = VBcp.CreateGenerator();
   string AssemblySourceFile = "MyClass.vb";
   string AssemblyFile = "MyClass.dll";
   TextWriter tw;
   tw = new StreamWriter(new FileStream(AssemblySourceFile, FileMode.Create));
   GenerateEvalMethod(tw, Expression, cg);
   ICodeCompiler cc = VBcp.CreateCompiler();
   CompilerParameters cpar = new CompilerParameters();
   cpar.OutputAssembly = AssemblyFile;
   cpar.GenerateExecutable = false;
   cpar.GenerateInMemory = false;
   CompilerResults compRes;
   compRes = cc.CompileAssemblyFromFile(cpar, AssemblySourceFile);
   Assembly ass;
   ass = compRes.CompiledAssembly;
   Type ty = ass.GetType("MyNamespace.MyClass");
   MethodInfo mi = ty.GetMethod("MyMethod");
   float Result = ((float)(mi.Invoke(null, null)));
   return Result;

 private static void GenerateEvalMethod(TextWriter tw, string Expression, ICodeGenerator CodeGen)
   CodeNamespace ns = new CodeNamespace("MyNamespace");
   CodeTypeDeclaration ty = new CodeTypeDeclaration("MyClass");
   ty.IsClass = true;
   ty.TypeAttributes = TypeAttributes.Public;
   CodeMemberMethod mm = new CodeMemberMethod();
   mm.Comments.Add(new CodeCommentStatement(String.Format("Expression:{0}", Expression)));
   mm.Name = "MyMethod";
   mm.ReturnType = new CodeTypeReference("System.Single");
   mm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
   mm.Statements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression(Expression)));
   CodeGen.GenerateCodeFromNamespace(ns, tw, null);

is this a class you wrote? is anything set to final? in otherwords you could write a class that implements the interface from that class and overload all the function calls and members then write in your own code to handle the situations to need to change. Just my $.02
jayrodAuthor Commented:
well i'm trying to write unitTests so that would get very messy after a while.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Sorry I misread your question , you have the sourcecode for the file? then what is the real question? and what does writing the unit testing matrix have to do with modifying the code in the module anyway? sorry its still a bit early in the day for me so I might be catching on slow.
jayrodAuthor Commented:
well.. I'm writing a .net add in that will create test methods for the user. I'm trying to be simple here but I want that the first time the user clicks the addin that the sourcefile and the test class are all generated. Now if the user is in the same file and on the same method I want to insert a new "method" into the already existing class. This is because i'm inserting stubs and not code. so I can't trample over the code that is already there.

make sense now?

I've got it figured out how to create the initial code, now i'm trying to figure out how to insert code. By looking at codedom so far it doesn't appear to have a code file read in that will let me mess around with th ecound under test
You might want to have a look at the System.Emit namespace. With this namespace you can dynamicly generate classes,methods,object and everything else at runtime. It lets you manipulate .cs files very easy.

More information about this can be found at:
jayrodAuthor Commented:
I found the answer. You have to use fileCodeModel in the visual studio object.

Thanks all for the help

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now