简体   繁体   中英

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?

Hello I have packed my standard code in a class dll. I am calling this dll from my C# service apps. But in the dll,at one point, there should be called a method with fixed name "func_X" that is not standard and has to be defined by the dll caller app. How can I realise this?

The challanging point is that the func_X is not called at a fix point in my dll. According to the flow, it is called at a different point.

My service where I call the 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
      }
   
    }
 }

My dll code

 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 My dll code

 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();
 }
       

You will to need pass the function to your dll program.

Update according to your latest edit:

Approach 1: You can pass your function to this constructor of Main_Prog and store it in a variable.

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();
    }
}

Approach 2: Instead of storing it globally pass the function to 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();
    }
}

In your Service1 pass func_x as an argument.

protected override void OnStart(string[] args)
{
  Concheetah_Class.Main_Prog main_Prog = new Concheetah_Class.Main_Prog();
  main_Prog.Main_Start(func_X);
}

In your Main_Prog receive it as an Action.

public void Main_Start(Action func_X)
{
  func_X();
}

Depending on your need you can switch between Func & Action . Action is used when the return type is void and Func is used when return type is not void.

I am not sure if I understand your question correctly but if I can take a stab at it, from what I am understanding you would like to create a method in your caller code than needs to be invoked by the code in your the dll that has already been build?

If so, I would use delegates to achieve this. You can add a parameter to your Main_Start method that accepts either a Action (void method) or Func (method with return type)

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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