
[英]How can I pass a function to a C# class dll, that I am calling from another C# app? The function is defined in the app where the dll is called
[英]In my C# class dll I want to call a method with fixed name "func_X", that should be placed at the C# app where the dll is called. How can I do that?
您好,我已将我的标准代码打包在 class dll 中。我从我的 C# 服务应用程序中调用此 dll。 但是在 dll 中,有一次应该调用一个固定名称为“func_X”的方法,该方法不是标准的,必须由 dll 调用者应用程序定义。 我怎样才能意识到这一点?
难点是func_X在我的dll中没有固定点调用,根据流程,是在不同的点调用。
我的服务电话是 dll
using Concheetah_Class; // my dll
namespace Concheetah_Service_Bahmuller
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Concheetah_Class.Main_Prog main_Prog = new Concheetah_Class.Main_Prog();
main_Prog.Main_Start(); // starting point of my dll
}
public void func_X()
{
// some supplementary code
}
}
}
我的 dll 密码
public void Main_Start()
{
// some long code
func_X(); // Here I should call the method that has to be defined on the caller side
// some long code
}
Update-1 我的 dll 代码
System.Timers.Timer timer1 = new System.Timers.Timer();
public void Main_Start()
{
Initialize_timer1();
}
public void Initialize_timer1()
{
timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent_timer1);
timer1 = 35;
timer1.Start();
}
private void OnTimedEvent_timer1(object sender, EventArgs e)
{
//some code
func_x();
}
您需要将 function 传递给您的 dll 程序。
根据您的最新编辑更新:
方法 1:您可以将 function 传递给Main_Prog
的构造函数并将其存储在变量中。
public class Main_Prog
{
System.Timers.Timer timer1 = new System.Timers.Timer();
Action func_x;
public Main_Prog(Action func_x)
{
this.func_x = func_x;
Initialize_timer1();
}
public void Initialize_timer1()
{
timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent_timer1);
timer1.Interval = 35;
timer1.Start();
}
private void OnTimedEvent_timer1(object sender, EventArgs e)
{
this.func_x();
}
}
方法 2:不是将其全局存储,而是将 function 传递给OnTimedEvent
:
public class Main_Prog
{
System.Timers.Timer timer1 = new System.Timers.Timer();
public Main_Prog(Action func_x)
{
Initialize_timer1(func_x);
}
public void Initialize_timer1(Action func_x)
{
timer1.Elapsed += (sender, args) => OnTimedEvent_timer1(sender, args, func_x);
timer1.Interval = 35;
timer1.Start();
}
private void OnTimedEvent_timer1(object sender, EventArgs e, Action func_x)
{
func_x();
}
}
在您的Service1
func_x
作为参数传递。
protected override void OnStart(string[] args)
{
Concheetah_Class.Main_Prog main_Prog = new Concheetah_Class.Main_Prog();
main_Prog.Main_Start(func_X);
}
在您的Main_Prog
其作为 Action 接收。
public void Main_Start(Action func_X)
{
func_X();
}
根据您的需要,您可以在Func和Action之间切换。 当返回类型为 void 时使用 Action,当返回类型不为 void 时使用 Func。
我不确定我是否正确理解了你的问题,但如果我可以尝试一下,根据我的理解,你想在你的调用者代码中创建一个方法,而不是你的 dll 中的代码需要调用的方法已经建好了?
如果是这样,我会使用代表来实现这一目标。 您可以向 Main_Start 方法添加一个参数,该方法接受 Action(无效方法)或 Func(具有返回类型的方法)
例子:
public class ActionExample // Delegate with NO return type
{
public void Run()
{
Main_Start(PrintName);
}
public void PrintName(string name)
{
Console.WriteLine($"My name is: {name}");
}
// Code in your packaged dll
// the string value in the generics represents the input value of the method
public void Main_Start(Action<string> methodToRun)
{
methodToRun("John Doe");
}
}
public class FuncExample // Delegate WITH return type
{
public void Run()
{
Main_Start(GetHelloMessage);
}
public string GetHelloMessage(string name)
{
return $"My name is: {name}";
}
// Code in your packaged dll
// First string in the generics represents input paramater and last string represents return paramater of the method
public void Main_Start(Func<string, string> methodToRun)
{
string message = methodToRun("John Doe");
Console.WriteLine(message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.