Saeed Parand
Saeed Parand

Reputation: 33

compile custom method in runtime c#

I'd like to compile a c# class or method in my project on runtime. I could compile my method on runtime but unfortunately I can not compile complex method run time or retrieved data from Database.

I use this code and it work well:

string sourceCode = @"
public class Calc_Class { 
public int Rent (int parameter , int a) {
int b= 20 ;
if(a+parameter > 10 ){ 
return parameter+3;
}
return parameter += 42;
}
}";
string sourceCode2 = @"
public class Calc_Class { 
public int Rent (int parameter , int a) {
int b= 20 ;
if(a+parameter > 10 ){ 

for(int i=0;i<5;i++){
parameter = parameter+i;
}
retrun parameter;
}
return parameter += 42;
}
}";
var compParms = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};
var csProvider = new CSharpCodeProvider();
CompilerResults compilerResults = csProvider.CompileAssemblyFromSource(compParms,    sourceCode);
 object typeInstance =  compilerResults.CompiledAssembly.CreateInstance("Calc_Class");
  MethodInfo mi = typeInstance.GetType().GetMethod("Rent");
  int methodOutput = (int)mi.Invoke(typeInstance, new object[] { 1, 19 });
  textBox2.Text = methodOutput.ToString();

if I call sourceCode it will work well but sourceCode2 has an error like : Could not load file or assembly 'file://AppData\Local\Temp\v4nza4bk.dll' or one of its dependencies. The system cannot find the file specified

after each run my project , the dll name will be changed. How can I fix this problem?

Upvotes: 0

Views: 103

Answers (2)

Theofrastos Mantadelis
Theofrastos Mantadelis

Reputation: 157

You have a typo on sourceCode2

retrun ---> return

indeed the message was not helping... but if you fix the typo your code works fine!

About you connection string errors: The following method has the source code pasted in... If you copy paste this in your C# you will see there are a lot of errors to fix. Fix those first and make it run fine as a method.

    void SourceCode3()
    {
        string connection = Data Source =.;
        Initial Catalog = TestReport;
        Integrated Security = True;
        using (SqlConnection con = new SqlConnection(connection))
        {
            using (SqlCommand cmd = new SqlCommand(SELECT * from FormulaOne))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    sdr.Read();
                    textBox2.Text = sdr[Formula].ToString();
                }
                con.Close();
            }
        }
    }

I would be expecting that you will reach to something like this:

        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = "Data Source=.; Initial Catalog=TestReport; Integrated Security = True;";
        con.Open();
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT * from FormulaOne");

        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        System.Data.SqlClient.SqlDataReader sdr = cmd.ExecuteReader();
        sdr.Read();
        textBox2.Text = sdr["Formula"].ToString();
        con.Close();

In order to make this a string remember that you need to put a \ before each ".

Hope this helps you

Upvotes: 1

Frenchy
Frenchy

Reputation: 17007

you could fix the name of dll compiled with CompilerParameters, you specify the OutputAssembly:

CompilerParameters compParms = new CompilerParameters();
compParms.OutputAssembly = @"d:\NameAssembly.dll";

or add the option during the initialization of compParms.

Upvotes: 1

Related Questions