event firing from codedom generated dll - how to join an event of a runtime generated dll form main exe application

43 Views Asked by At

I have some user inputs of type string[] for calculations. I want to compile them and get faster computation than any other method.

User input strings:

double _1 = 2 + 2;
double _2 = _1 + Get_Variable("var1");
return _2;

I convert the input to strings which are suitable for generating a dll

namespace namespace_Calculator
{
    public class Class_Calculator
    {  
        public static double Start_Calculating()
        {
           //user defined string[] type calculation inputs
           double _1 = 2 + 2;
           double _2 = _1 + Get_Variable("var1");

            return _2;
           //user defined string[] type calculation inputs
        }
    }
}

How can I return the Get_Variable() function from main exe? I'm currently using DataTable().Compute()

I can call Start_Calculating() and get result without variables.

object[] p = new object[] { };
Assembly yeni = AppDomain.CurrentDomain.Load(File.ReadAllBytes(Dll_location));
Type t = yeni.GetType("namespace_Calculator.Class_Calculator");
MethodInfo mi = t.GetMethod("Start_Calculating");
object ins = Activator.CreateInstance(t);
double cevap = (double)mi.Invoke(ins, p);

Thanks in advance.

1

There are 1 best solutions below

0
ArgeMuP On

The solution is easier than it seems.

  • On .exe file codeDom side:

It is enough to add "Name.exe" to CompilerParameters.ReferencedAssemblies object.

CompilerParameters options = new CompilerParameters();
...
options.ReferencedAssemblies.Add(AppDomain.CurrentDomain.FriendlyName);
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerResults results = provider.CompileAssemblyFromSource(options, SourceCode);
  • On .exe file callable function side:

Create a standart static public function

namespace ns_Callable
{
    public class cl_Callable
    {
        public static double Get_Variable(string Input)
        {
            return 0;
        }
    }
}
  • On newly created .dll file side:

Nothing special

namespace namespace_Calculator
{
    public class Class_Calculator
    {  
        public static double Start_Calculating()
        {
           //user defined string[] type calculation inputs
           double _1 = 2 + 2;
           double _2 = _1 + ns_Callable.cl_Callable.Get_Variable("var1");

            return _2;
           //user defined string[] type calculation inputs
        }
    }
}