<p>I am new to all the anonymous features and need some help.  I have gotten the following to work:</p>

<pre><code>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));
}
</code></pre>

<p>But this is totally ugly and I would like to have the inside of the Do to be an anonymous method or even a lambda if it is possible.  I tried:</p>

<pre><code>Expect.Call(delegate { _dao.Save(t); }).Do(delegate(Transaction t2) { t2.Message = "I drink goats blood"; });
</code></pre>

<p>and</p>

<pre><code>Expect.Call(delegate { _dao.Save(t); }).Do(delegate { t.Message = "I drink goats blood"; });
</code></pre>

<p>but these give me</p>

<blockquote>
  <p>Cannot convert anonymous method to type 'System.Delegate' because it is not a delegate type** compile errors.</p>
</blockquote>

<p>What am I doing wrong?</p>

<hr>

<p>Because of what Mark Ingram posted, seems like the best answer, though nobody's explicitly said it, is to do this:</p>

<pre><code>public delegate void FakeSave(Transaction t);

Expect.Call(delegate { _dao.Save(t); }).Do( new FakeSave(delegate(Transaction t2) { t.Message = expected_msg; }));
</code></pre>

[{'answer_id': 59531, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 0, 'selected': False, 'text': '<p>Try something like:</p>\n\n<pre><code>Expect.Call(delegate { _dao.Save(t); }).Do(new EventHandler(delegate(Transaction t2) { t2.CheckInInfo.CheckInMessage = "I drink goats blood"; }));\n</code></pre>\n\n<p>Note the added EventHandler around the delegate.</p>\n\n<p>EDIT: might not work since the function signatures of EventHandler and the delegate are not the same...  The solution you added to the bottom of your question may be the only way.</p>\n\n<p>Alternately, you could create a generic delegate type:</p>\n\n<pre><code>public delegate void UnitTestingDelegate&lt;T&gt;(T thing);\n</code></pre>\n\n<p>So that the delegate is not Transaction specific.</p>\n'}, {'answer_id': 59551, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 6, 'selected': True, 'text': '<p>That\'s a well known error message. Check the link below for a more detailed discussion.</p>\n\n<p><a href="http://staceyw1.wordpress.com/2007/12/22/they-are-anonymous-methods-not-anonymous-delegates/" rel="noreferrer">http://staceyw1.wordpress.com/2007/12/22/they-are-anonymous-methods-not-anonymous-delegates/</a> </p>\n\n<p>Basically you just need to put a cast in front of your anonymous delegate (your lambda expression).</p>\n\n<p>In case the link ever goes down, here is a copy of the post:  </p>\n\n<blockquote>\n  <p><strong>They are Anonymous Methods, not\n  Anonymous Delegates.</strong><br>\n  Posted on December 22, 2007 by staceyw1  </p>\n  \n  <p>It is not just a talking point because\n  we want to be difficult. It helps us\n  reason about what exactly is going on.\n  To be clear, there is *no such thing\n  as an anonymous delegate. They don’t\n  exist (not yet).  They are "Anonymous\n  Methods" – period.  It matters in how\n  we think of them and how we talk about\n  them.  Lets take a look at the\n  anonymous method statement "delegate()\n  {…}".  This is actually two different\n  operations and when we think of it\n  this way, we will never be confused\n  again.  The first thing the compiler\n  does is create the anonymous method\n  under the covers using the inferred\n  delegate signature as the method\n  signature.  It is not correct to say\n  the method is "unnamed" because it\n  does have a name and the compiler\n  assigns it. It is just hidden from\n  normal view.  The next thing it does\n  is create a delegate object of the\n  required type to wrap the method. This\n  is called delegate inference and can\n  be the source of this confusion. For\n  this to work, the compiler must be\n  able to figure out (i.e. infer) what\n  delegate type it will create. It has\n  to be a known concrete type.  Let\n  write some code to see why.</p>\n</blockquote>\n\n<pre><code>private void MyMethod()\n{\n}\n</code></pre>\n\n<blockquote>\n  <p><strong>Does not compile:</strong>  </p>\n</blockquote>\n\n<pre><code>1) Delegate d = delegate() { };                       // Cannot convert anonymous method to type ‘System.Delegate’ because it is not a delegate type\n2) Delegate d2 = MyMethod;                         // Cannot convert method group ‘MyMethod’ to non-delegate type ‘System.Delegate’\n3) Delegate d3 = (WaitCallback)MyMethod;   // No overload for ‘MyMethod’ matches delegate ‘System.Threading.WaitCallback’\n</code></pre>\n\n<blockquote>\n  <p>Line 1 does not compile because the\n  compiler can not infer any delegate\n  type. It can plainly see the signature\n  we desire, but there is no concrete\n  delegate type the compiler can see. \n  It could create an anonymous type of\n  type delegate for us, but it does not\n  work like that.  Line 2 does not\n  compile for a similar reason. Even\n  though the compiler knows the method\n  signature, we are not giving it a\n  delegate type and it is not just going\n  to pick one that would happen to work\n  (not what side effects that could\n  have).  Line 3 does not work because\n  we purposely mismatched the method\n  signature with a delegate having a\n  different signature (as WaitCallback\n  takes and object).</p>\n  \n  <p><strong>Compiles:</strong>  </p>\n</blockquote>\n\n<pre><code>4) Delegate d4 = (MethodInvoker)MyMethod;  // Works because we cast to a delegate type of the same signature.\n5) Delegate d5 = (Action)delegate { };              // Works for same reason as d4.\n6) Action d6 = MyMethod;                                // Delegate inference at work here. New Action delegate is created and assigned.\n</code></pre>\n\n<blockquote>\n  <p>In contrast, these work. Line 1 works\n  because we tell the compiler what\n  delegate type to use and they match,\n  so it works.  Line 5 works for the\n  same reason. Note we used the special\n  form of "delegate" without the parens.\n  The compiler infers the method\n  signature from the cast and creates\n  the anonymous method with the same\n  signature as the inferred delegate\n  type. Line 6 works because the\n  MyMethod() and Action use same\n  signature.</p>\n  \n  <p>I hope this helps.</p>\n  \n  <p>Also see:\n  <a href="http://msdn.microsoft.com/msdnmag/issues/04/05/C20/" rel="noreferrer">http://msdn.microsoft.com/msdnmag/issues/04/05/C20/</a></p>\n</blockquote>\n'}, {'answer_id': 59568, 'author': 'hwiechers', 'author_id': 5883, 'author_profile': 'https://Stackoverflow.com/users/5883', 'pm_score': 2, 'selected': False, 'text': '<p>What Mark said.</p>\n\n<p>The problem is that Do takes a Delegate parameter. The compiler can\'t convert the anonymous methods to Delegate, only a "delegate type" i.e. a concrete type derived from Delegate.</p>\n\n<p>If that Do function had took Action&lt;>, Action&lt;,> ... etc. overloads, you wouldn\'t need the cast.</p>\n'}, {'answer_id': 59611, 'author': 'Brannon', 'author_id': 5745, 'author_profile': 'https://Stackoverflow.com/users/5745', 'pm_score': 1, 'selected': False, 'text': '<p>The problem is not with your delegate definition, it\'s that the parameter of the Do() method is of type System.Delegate, and the compiler generated delegate type (FakeSave) does not implicitly convert to System.Delegate.</p>\n\n<p>Try adding a cast in front of your anonymous delegate:</p>\n\n<pre><code>Expect.Call(delegate { _dao.Save(t); }).Do((Delegate)delegate { t.Message = "I drink goats blood"; });\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/59515', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5056/']