anonymous-methods

C# to VB - How do I convert this anonymous method / lambda expression across?

。_饼干妹妹 提交于 2019-12-13 14:25:50
问题 How would you convert this to VB (using .NET 4.0 / VS2010) ? bw.DoWork += (o, args) => { Code Here }; I thought maybe like this: AddHandler bw.DoWork, Function(o, args) Code Here End Function But it says Function does not return a value on all code paths. Ideas? 回答1: VB.NET lacks this Interesting bits: (..)VB does not have anonymous methods, only Lambda expressions (no way to declare an anonymous Action delegate). 回答2: You can't make it that way, but you can make it work like: Sub New()

How to avoid anonymous methods in “dynamic” event subscription?

天涯浪子 提交于 2019-12-12 10:47:16
问题 How could I refactor the method private void ListenToPropertyChangedEvent(INotifyPropertyChanged source, string propertyName) { source.PropertyChanged += (o, e) => { if (e.PropertyName == propertyName) MyMagicMethod(); }; } if I wished to avoid using the anonymous method here? 回答1: Implement the closure that is implicitly created by the lambda explicitly: private void ListenToPropertyChangedEvent(INotifyPropertyChanged source, string propertyName) { var listener = new

What do permit anonymous parameterless delegate types to differ?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 08:14:08
问题 Having read in article "Anonymous Methods" (as part of the articles series "Delegates and Lambda Expressions in C# 3.0") the phrase: " Advanced Topic: Parameterless Anonymous Methods ... anonymous methods are allowed to omit the parameter list ( delegate { return Console.ReadLine() != ""} , for example). This is atypical, but it does allow the same anonymous method to appear in multiple scenarios even though the delegate type may vary "* I became a little confused. IMO (can't find now but as

How can I extract the “real” target from an Event Handler that was defined anonymously?

我只是一个虾纸丫 提交于 2019-12-11 10:47:50
问题 Follow up to my question here: According to a comment made - The compiler creates a class member with a fictitious name and attaches it just as you would attach a declared method. I do not fully comprehend what this means but I can verify that if instead of saying Foo.Bar += (S, E) => { /*Code Goes Here*/ } I instead say Foo.Bar += FooBar; private void FooBar( object sender, EventArgs e ){ /*Code Goes Here*/ } then Event.Target changes from WhatIsThis.App.<>c to WhatIsThis.App . That's

can't reference non-static method from anonymous method

大兔子大兔子 提交于 2019-12-11 07:37:11
问题 i need to call a non-static method from an async operation , for ease i'm using apm design , by defining a delegate assign it an anonymous method and calling beginInvoke on it . to my surprise i could not reference a non-static method from my implementation any idea why that is ? public delegate void UpdatePlayersLogin(IServerCallback callback, Guid callback_playerId, Player player, List<IServerCallback> toRemove, ManualResetEvent handel); [ServiceBehavior(ConcurrencyMode = ConcurrencyMode

Anonymous method as function result

。_饼干妹妹 提交于 2019-12-10 21:04:46
问题 What I want to do is to assign an anonymous method which I get as a function result to a variable of the same type. Delphi complains about not beeing able to do the assignement. Obviously Delphi things I want to assign the "GetListener" function instead of the result of that same function. Any help with this is very much appreciated. type TPropertyChangedListener = reference to procedure (Sender: TStimulus); TMyClass = class function GetListener:TPropertyChangedListener end; .... var MyClass:

Private field captured in anonymous delegate

柔情痞子 提交于 2019-12-09 05:19:24
问题 class A { public event EventHandler AEvent; } class B { private A _foo; private int _bar; public void AttachToAEvent() { _foo.AEvent += delegate() { ... UseBar(_bar); ... } } } Since delegate captures variable this._bar , does it implicitly hold to the instance of B ? Will instance of B be referenced through the event handler and captured variable by an instance of A ? Would it be different if _bar was a local variable of the AttachToAEvent method? Since in my case an instance of A lives far

VCL events with anonymous methods - what do you think about this implementation?

安稳与你 提交于 2019-12-08 23:39:18
问题 Since anonymous methods appeared in Delphi I wanted to use them in VCL components events. Obviously for backward compatibility the VCL wasn't updated, so I managed to make a simple implementation with a few caveats. type TNotifyEventDispatcher = class(TComponent) protected FClosure: TProc<TObject>; procedure OnNotifyEvent(Sender: TObject); public class function Create(Owner: TComponent; Closure: TProc<TObject>): TNotifyEvent; overload; function Attach(Closure: TProc<TObject>): TNotifyEvent;

ThreadPool.QueueUserWorkItem with a lambda expression and anonymous method

こ雲淡風輕ζ 提交于 2019-12-08 23:13:12
问题 Passing two parameters to a new thread on the threadpool can sometimes be complicated, but it appears that with lambda expressions and anonymous methods, I can do this: public class TestClass { public void DoWork(string s1, string s2) { Console.WriteLine(s1); Console.WriteLine(s2); } } try { TestClass test = new TestClass(); string s1 = "Hello"; string s2 = "World"; ThreadPool.QueueUserWorkItem( o => test.DoWork(s1, s2) ); } catch (Exception ex) { //exception logic } Now, I've certainly

Replace Property Getter/Setter with Reflection or similar (no 3rd party libraries) in c#

此生再无相见时 提交于 2019-12-08 21:02:01
问题 I've got a property contained in a class for example public class Greeter { private Hashtable _data; public string HelloPhrase { get; set; } public Greeter(data) { _data = data; } } What I would like to do is add an Attribute to the HelloPhrase property, like this [MyCustomAttribute("Hello_Phrase")] public string SayHello { get; set; } Such that during the constructor I can reflect over the Properties in the Class(Greeter) where MyCustomAttribute has been defined and set the Get/Set methods