c# - Convert this delegate to an anonymous method or lambda -


i new anonymous features , need help. have gotten following work:

public void fakesavewithmessage(transaction t) {     t.message = "i drink goats blood"; }  public delegate void fakesave(transaction t);  public void sampletestfunction() {     expect.call(delegate { _dao.save(t); }).do(new fakesave(fakesavewithmessage)); } 

but totally ugly , have inside of anonymous method or lambda if possible. tried:

expect.call(delegate { _dao.save(t); }).do(delegate(transaction t2) { t2.message = "i drink goats blood"; }); 

and

expect.call(delegate { _dao.save(t); }).do(delegate { t.message = "i drink goats blood"; }); 

but these give me

cannot convert anonymous method type 'system.delegate' because not delegate type** compile errors.

what doing wrong?


because of mark ingram posted, seems best answer, though nobody's explicitly said it, this:

public delegate void fakesave(transaction t);  expect.call(delegate { _dao.save(t); }).do( new fakesave(delegate(transaction t2) { t.message = expected_msg; })); 

that's known error message. check link below more detailed discussion.

http://staceyw1.wordpress.com/2007/12/22/they-are-anonymous-methods-not-anonymous-delegates/

basically need put cast in front of anonymous delegate (your lambda expression).

in case link ever goes down, here copy of post:

they anonymous methods, not anonymous delegates.
posted on december 22, 2007 staceyw1

it not talking point because want difficult. helps reason going on. clear, there *no such thing anonymous delegate. don’t exist (not yet). "anonymous methods" – period. matters in how think of them , how talk them. lets take @ anonymous method statement "delegate() {…}". 2 different operations , when think of way, never confused again. first thing compiler create anonymous method under covers using inferred delegate signature method signature. not correct method "unnamed" because have name , compiler assigns it. hidden normal view. next thing create delegate object of required type wrap method. called delegate inference , can source of confusion. work, compiler must able figure out (i.e. infer) delegate type create. has known concrete type. let write code see why.

private void mymethod() { } 

does not compile:

1) delegate d = delegate() { };                       // cannot convert anonymous method type ‘system.delegate’ because not delegate type 2) delegate d2 = mymethod;                         // cannot convert method group ‘mymethod’ non-delegate type ‘system.delegate’ 3) delegate d3 = (waitcallback)mymethod;   // no overload ‘mymethod’ matches delegate ‘system.threading.waitcallback’ 

line 1 not compile because compiler can not infer delegate type. can plainly see signature desire, there no concrete delegate type compiler can see. create anonymous type of type delegate us, not work that. line 2 not compile similar reason. though compiler knows method signature, not giving delegate type , not going pick 1 happen work (not side effects have). line 3 not work because purposely mismatched method signature delegate having different signature (as waitcallback takes , object).

compiles:

4) delegate d4 = (methodinvoker)mymethod;  // works because cast delegate type of same signature. 5) delegate d5 = (action)delegate { };              // works same reason d4. 6) action d6 = mymethod;                                // delegate inference @ work here. new action delegate created , assigned. 

in contrast, these work. line 1 works because tell compiler delegate type use , match, works. line 5 works same reason. note used special form of "delegate" without parens. compiler infers method signature cast , creates anonymous method same signature inferred delegate type. line 6 works because mymethod() , action use same signature.

i hope helps.

also see: http://msdn.microsoft.com/msdnmag/issues/04/05/c20/


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -