![](/img/trans.png)
[英]How to subscribe to a event from a dll file and get a list generated by the dll?
[英]event firing from codedom generated dll - how to join an event of a runtime generated dll form main exe application
我有一些string[]
類型的用戶輸入用於計算。 我想編譯它們並獲得比任何其他方法更快的計算。
用戶輸入字符串:
double _1 = 2 + 2;
double _2 = _1 + Get_Variable("var1");
return _2;
我將輸入轉換為適合生成 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
}
}
}
如何從主 exe 返回Get_Variable()
函數? 我目前正在使用DataTable().Compute()
我可以調用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);
提前致謝。
解決方案比看起來容易。
將“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);
創建一個標准的靜態公共函數
namespace ns_Callable
{
public class cl_Callable
{
public static double Get_Variable(string Input)
{
return 0;
}
}
}
沒什么特別的
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.