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

x
  • 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?
0
jayrod
Asked:
jayrod
2 Solutions
 
Thandava VallepalliCommented:
http://www.codeproject.com/dotnet/CodeFromCode.asp


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


http://www.developerfusion.com/utilities/convertvbtocsharp.aspx

==============================
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("--------------------------");
   Console.WriteLine("Type an arithmetic expression: ");
   string Expression = Console.ReadLine();
   Console.WriteLine("Your expression is evaluated as: ");
   Console.WriteLine(ProcessExpression(Expression));
   Console.ReadLine();
 }

 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);
   tw.Close();
   ICodeCompiler cc = VBcp.CreateCompiler();
   CompilerParameters cpar = new CompilerParameters();
   cpar.OutputAssembly = AssemblyFile;
   cpar.ReferencedAssemblies.Add("System.dll");
   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;
   ns.Types.Add(ty);
   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)));
   ty.Members.Add(mm);
   CodeGen.GenerateCodeFromNamespace(ns, tw, null);
 }
}
=============

itsvtk
0
 
talspaugh27Commented:
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
0
 
jayrodAuthor Commented:
well i'm trying to write unitTests so that would get very messy after a while.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

 
talspaugh27Commented:
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.
0
 
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
0
 
existenz2Commented:
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:
http://www.ondotnet.com/pub/a/dotnet/excerpt/prog_csharp_ch18/index.html?page=7
0
 
jayrodAuthor Commented:
I found the answer. You have to use fileCodeModel in the visual studio object.

Thanks all for the help
0

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