How should I write the .i file to wrap callbacks in Java or C#

后端 未结 3 1393
孤独总比滥情好
孤独总比滥情好 2020-11-28 12:38

My C program uses callback functions which are periodically called. I want to be able to handle the callback functions in a Java or C# program. How should I write the .i fil

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 12:47

    C# solution : you must use delegates. look at the code sample

    public delegate void DoSome(object sender);
    
    public class MyClass{
          public event DoSome callbackfunc;
    
          public void DoWork(){
                // do work here 
                if(callbackfunc != null)
                         callbackfunc(something);
          }
    }
    

    this is also like event handling mechanism but by theory both of have same implementation in c#

    Java solution: you must use interfaces, look at this sample code

    interface Notification{
          public void somthingHappend(Object obj);
    }
    
    class MyClass{
         private Notification iNotifer;
    
         public void setNotificationReciver(Notification in){
                this.iNotifier = in;
         }
    
         public void doWork(){
                //some work to do
                if(something heppens){ iNotifier.somthingHappend(something);}
         }
    }
    

    actually you can use a List of Notification to implement a set of callback receivers

提交回复
热议问题