简体   繁体   English

从 codedom 生成的 dll 触发事件 - 如何加入运行时生成的 dll 表单主 exe 应用程序的事件

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

I have some user inputs of type string[] for calculations.我有一些string[]类型的用户输入用于计算。 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我将输入转换为适合生成 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?如何从主 exe 返回Get_Variable()函数? I'm currently using DataTable().Compute()我目前正在使用DataTable().Compute()

I can call Start_Calculating() and get result without variables.我可以调用Start_Calculating()并获得没有变量的结果。

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.提前致谢。

The solution is easier than it seems.解决方案比看起来容易。

  • On .exe file codeDom side:在 .exe 文件 codeDom 端:

It is enough to add "Name.exe" to CompilerParameters.ReferencedAssemblies object.将“Name.exe”添加到 CompilerParameters.ReferencedAssemblies 对象就足够了。

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:在 .exe 文件可调用函数端:

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:在新创建的 .dll 文件端:

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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM