Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi..
 
How to do compile and run a application dynamically
Posted 20-Feb-13 21:40pm
Comments
CodeMaster_Noob at 21-Feb-13 2:43am
   
provide some details on your problem... what sort of issues are you facing while achieving your task..??
CShubham at 22-Feb-13 2:02am
   
clear your question????
Shubh Agrahari at 22-Feb-13 4:05am
   
ya actually what you want to achieve...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

To compile code dynamically, have a look at the CSharpCodeProvider[^].
  Permalink  
Comments
Espen Harlinn at 24-Feb-13 6:09am
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
Search for CodeDomProvider in google.
 
Thanks
--RA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

hi professional .
 
this link may help you that you want to do....
 
http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments[^]
 

Happy to Help..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

import java.io.*;
import java.util.*;
import java.lang.reflect.*;
 
public class MakeTodayClass {
  Date today = new Date();
  String todayMillis = Long.toString(today.getTime());
  String todayClass = "z_" + todayMillis;
  String todaySource = todayClass + ".java";
  
  public static void main (String args[]){
    MakeTodayClass mtc = new MakeTodayClass();
    mtc.createIt();
    if (mtc.compileIt()) {
       System.out.println("Running " + mtc.todayClass + ":\n\n");
       mtc.runIt();
       }
    else
       System.out.println(mtc.todaySource + " is bad.");
    }
 
  public void createIt() {
    try {
      FileWriter aWriter = new FileWriter(todaySource, true);
      aWriter.write("public class "+ todayClass + "{");
      aWriter.write(" public void doit() {");
      aWriter.write(" System.out.println(\""+todayMillis+"\");");
      aWriter.write(" }}\n");
      aWriter.flush();      
      aWriter.close();
      }
    catch(Exception e){
      e.printStackTrace();
      }
    }
  
  public boolean compileIt() {
    String [] source = { new String(todaySource)};
    ByteArrayOutputStream baos= new ByteArrayOutputStream();
 
    new sun.tools.javac.Main(baos,source[0]).compile(source);
    // if using JDK >= 1.3 then use
    //   public static int com.sun.tools.javac.Main.compile(source);    
    return (baos.toString().indexOf("error")==-1);
    }
    
  public void runIt() {
    try {
      Class params[] = {};
      Object paramsObj[] = {};
      Class thisClass = Class.forName(todayClass);
      Object iClass = thisClass.newInstance();
      Method thisMethod = thisClass.getDeclaredMethod("doit", params);
      thisMethod.invoke(iClass, paramsObj);
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    }
 }
  Permalink  
v2
Comments
CShubham at 22-Feb-13 2:01am
   
hi!!! make confirm what is the question first!!!
_Amy at 22-Feb-13 3:16am
   
Question tag is "Asp.Net" & "C#".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Zoltán Zörgő 220
1 Sergey Alexandrovich Kryukov 180
2 Peter Leow 160
3 BillWoodruff 85
4 bling 80
0 Sergey Alexandrovich Kryukov 9,543
1 OriginalGriff 6,901
2 Peter Leow 4,737
3 Zoltán Zörgő 4,404
4 CHill60 2,932


Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100