Home > front end >  In my C# class dll I want to call a method with fixed name "func_X", that should be placed
In my C# class dll I want to call a method with fixed name "func_X", that should be placed

Time:01-13

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

CodePudding user response:

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

CodePudding user response:

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

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.

  • Related