delegates

Unable to use custom class in a protocol with @objc attribute?

廉价感情. 提交于 2019-12-18 19:39:22
问题 I am trying to create a protocol for JSON loading delegation, JSONLoaderDelegate . My other class, called JSONLoader , is supposed to dispatch events to its delegate (that implements the JSONLoaderDelegate protocol) like: self?.delegate?.jsonLoaderdidEndWithError(self!, error: JSONLoaderError.LoadError) The implementation of the JSONLoader is not that important (imho). However I seem to have problems to implement the protocol, this is the code: @objc protocol JSONLoaderDelegate { optional

Lambda\Anonymous Function as a parameter

时光总嘲笑我的痴心妄想 提交于 2019-12-18 19:03:26
问题 I'm a very new to C#. Just playing around with it. Not for a real purpose. void makeOutput( int _param) { Console.WriteLine( _param.ToString()); } //... // Somewhere in a code { makeOutput( /* some not c# code for an example for what do I want */ function : int () { return 0; } ); } Is it possible to use a REAL anonymous functions (means returning result)? I do not want to use delegates such as // Somewhere in a code { Func<int> x = () => { return 0; }; makeOutput( x()) } Also I DO NOT want

Delegate Usage : Business Applications

岁酱吖の 提交于 2019-12-18 16:59:30
问题 Background Given that 'most' developers are Business application developers, the features of our favorite programming languages are used in the context of what we're doing with them. As a C# / ASP.NET Application developer, I tend to only use delegates when dealing with UI events. In fact (and this is part of my inexperience showing), I don't even know a good context other than events to use delegates in! This is quite scary; but I'm gathering that there are other developers in the same boat.

Why can't a delegate refer to a non-static method when used in a static method?

青春壹個敷衍的年華 提交于 2019-12-18 16:47:12
问题 Why is it necessary to make a function STATIC while using delegates in C# ? class Program { delegate int Fun (int a, int b); static void Main(string[] args) { Fun F1 = new Fun(Add); int Res= F1(2,3); Console.WriteLine(Res); } **static public int Add(int a, int b)** { int result; result = a + b; return result; } } 回答1: It's not "necessary". But your Main method is static , so it can't call a non- static method. Try something like this (this isn't really a good way to do things—you really

Delegate Covariance Confusion Conundrum!

自作多情 提交于 2019-12-18 15:52:08
问题 Why does this not work? Do I not understand delegate covariance correctly? public delegate void MyDelegate(object obj) public class MyClass { public MyClass() { //Error: Expected method with 'void MyDelegate(object)' signature _delegate = MyMethod; } private MyDelegate _delegate; public void MyMethod(SomeObject obj) {} } 回答1: Correct - you don't understand covariance correctly - yet :) Your code would work if you had the same types but as return values, like this: public delegate object

Reflection - Add a Delegate to another Delegate's invocation list

时光怂恿深爱的人放手 提交于 2019-12-18 15:49:49
问题 i am attempting to attach a Delegate to an invocation list of a different delegate. By that i am achieving a kind of Hook on existing events. I need to hook up something that runs after each event that is invoked. The following example works as long as the Delegate exposed by the type and the Action i pass in have the exact same signature. (On1 and OnAll events are both declared with an Action delegate so it works). Code : How i hook up an Action with an existing delegate exposed by an event

swift delegate beetween two view controller without segue

半世苍凉 提交于 2019-12-18 13:33:29
问题 My delegate protocol never called My first controller - ViewController class ViewController: UIViewController,testProtocol { @IBAction func btInit(sender: AnyObject) { println("Bt Init") let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as targetViewController self.presentViewController(initViewController,animated: false, nil) } var targetController =

Func delegate doesn't chain methods

蓝咒 提交于 2019-12-18 13:07:16
问题 Lets imagine simple delegate calls: void Main() { Func<int, int, string> tfunc = null; tfunc += Add; // bind first method tfunc += Sub; // bind second method Console.WriteLine(tfunc(2, 2)); } private string Add(int a, int b) { return "Add: " + (a + b).ToString(); } private string Sub(int a, int b) { return "Sub: " + (a - b).ToString(); } The result of this program is: Sub: 0 So, why Add method was not called? I'm expecting to call Method Add, and then method Sub. 回答1: Add was correctly

+= operator for Delegate

独自空忆成欢 提交于 2019-12-18 12:49:24
问题 I know that the += operator will add a method to the invocation list maintained by the Delegate base object, for example using System; class Program { delegate void MyDelegate(int n); void Foo(int n) { Console.WriteLine("n = {0}", n) } static void Main(string[] args) { MyDelegate d = new MyDelegate(Foo); d += Foo; // add Foo again d.Invoke(3); // Foo is invoked twice as Foo appears two times in invocation list } } But when I look at MSDN Delegate, MulticastDelegate I can't find any definition

Subscribe to events within a WCF service

你。 提交于 2019-12-18 12:26:48
问题 I have a need to do some real-time reporting on the functionality of a WCF service. The service is self-hosted in a windows app, and my requirement is to report "live" to the host app when certain methods are called by the client. My initial thought on the task was to publish a "NotifyNow" event in the service code, and subscribe to the event in my calling app, but this doesn't seem to be possible. Within my service code (implementation, not the interface) I have tried adding the following