delegates

Mocking EventHandler

时光总嘲笑我的痴心妄想 提交于 2019-12-23 11:52:57
问题 Having defined an interface public interface IHandlerViewModel { EventHandler ClearInputText { get; } } I would like to test if ClearInputText is invoked by some method. To do so I do something like this SomeType obj=new SomeType(); bool clearCalled = false; var mockHandlerViewModel=new Mock<IHandlerViewModel>(); mockHandlerViewModel.Setup(x => x.ClearInputText).Returns(delegate { clearCalled = true; }); obj.Call(mockHandlerViewModel.Object);//void Call(IHandlerViewModel); Assert.IsTrue

Mocking EventHandler

泄露秘密 提交于 2019-12-23 11:52:10
问题 Having defined an interface public interface IHandlerViewModel { EventHandler ClearInputText { get; } } I would like to test if ClearInputText is invoked by some method. To do so I do something like this SomeType obj=new SomeType(); bool clearCalled = false; var mockHandlerViewModel=new Mock<IHandlerViewModel>(); mockHandlerViewModel.Setup(x => x.ClearInputText).Returns(delegate { clearCalled = true; }); obj.Call(mockHandlerViewModel.Object);//void Call(IHandlerViewModel); Assert.IsTrue

Why does using Delegate.Combine require a cast but using the + operator doesn't?

点点圈 提交于 2019-12-23 11:48:36
问题 I couldn't find the + operator in Reflector under Delegate or MulticastDelegate . I'm trying to figure out how this doesn't need a cast: Action a = () => Console.WriteLine("A"); Action b = () => Console.WriteLine("B"); Action c = a + b; But this does: Action a = () => Console.WriteLine("A"); Action b = () => Console.WriteLine("B"); Action c = (Action)MulticastDelegate.Combine(a, b); In the first sample is the cast just done under the covers? 回答1: Taking the following example: class Program {

Does C# Pass by Value to Lambdas?

半腔热情 提交于 2019-12-23 10:56:46
问题 I have some code, int count = 0; list.ForEach(i => i.SomeFunction(count++)); This seems to not increment count. Is count passed by value here? Is there any difference if I use the {} in the lambda? int count = 0; list.ForEach(i => { i.SomeFunction(count++); }); Update 1 Sorry, my mistake, it does update the original count. 回答1: count is an int, and ints are value types, which means they are indeed passed by value. There is no semantic difference between your first and second example. (That

How to send image from framework to sample project in iOS Swift?

十年热恋 提交于 2019-12-23 10:03:16
问题 I have implemented document scanner using visionKit. Initially i have faced camera dismiss issue and it fixed. Now i am trying to send image after camera dismiss from framework to sample project. I have tried using completion handler, but it does not work. Here is the code for framework: public class A8Scan: NSObject, VNDocumentCameraViewControllerDelegate { var imageNew: UIImage? var statusImage: UIImageView? private var clientView: UIViewController? public init(_ viewController

Asp.net: Can a delegate (“Action”) be serialized into control state?

安稳与你 提交于 2019-12-23 09:31:37
问题 I am implementing a user control that has a method that takes an Action delegate as a parm. Attempting to store the delegate in Control State yields a serialization error. Is it even possible to serialize a delegate into Control State? BP 回答1: Not easily - and it could open the door for potential problems. It is theoretically possible to use reflection to determine which method of an object the delegate is invoking, and write a custom serialization process for it. Upon deserialization you

Passing delegate as method parameter

≡放荡痞女 提交于 2019-12-23 09:29:08
问题 I'm currently developing an EventManager class to ensure that no events are left wired to dead WCF duplex clients, and also to control prevent multiple wiring from the same client to the one event. Now basically, I'm what stuck with is trying to pass the event delegate to a function that will control the assignment like this. var handler = new SomeEventHandler(MyHandler); Wire(myObject.SomeEventDelegate, handler); To call this: private void Wire(Delegate eventDelegate, Delegate handler) { //

MKAnnotationView viewForAnnotation never called

橙三吉。 提交于 2019-12-23 08:57:41
问题 after i spend 2 days of searching the bug i have to ask for help here. i do have MapViewController and place some pins on the map. i´ve copied most of the code from MapCallouts and WeatherMap of the apple code samples. anyhow it seems that i´ve deleted or missed essential parts. it seems there is no connection between the MapViewController and the following code - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation { NSLog(@"MKAnnotationView");

How to pass an arbitrary method (or delegate) as parameter to a function?

别说谁变了你拦得住时间么 提交于 2019-12-23 08:17:43
问题 I need to be able to pass an arbitrary method to some function myFunction : void myFunction(AnyFunc func) { ... } It should be possible to execute it with other static, instance, public or private methods or even delegates: myFunction(SomeClass.PublicStaticMethod); myFunction(SomeObject.PrivateInstanceMethod); myFunction(delegate(int x) { return 5*x; }); Passed method may have any number of parameters and any return type. It should also be possible to learn the actual number of parameters and

What is the closest thing Java has to the .NET Func<> and Action<> delegates?

落花浮王杯 提交于 2019-12-23 08:00:07
问题 Obviously, Java does not have delegates or functions as first class values and uses interfaces instead, but what is the closest interface to the Func<> or Action<> .NET delegates? There is Runnable and Callable<>, but only in flavors taking no parameters. As Java cannot have overloaded types with the same name and different number of generic type arguments, I understand that there cannot be a single shared interface name, but there could be a Runnable1<>, Runnable2<> and so on. Is this style