I'm having some difficulty with an overloaded method. My signatures look like this:
void Register开发者_StackOverflow(object source, EventHandler mapped_handler)
void Register(object source, string mapped_property)
I'm trying to call the first one like this:
Register(someObject, (s, e) => { ... });
Visual Studio 2008 is giving me error CS1503: Argument '2': cannot convert from 'System.EventHandler' to 'string'
.
What am I missing? I'm not really sure where the problem is. I have other non-overloaded methods which I can pass EventHandler
lambdas to without a problem.
That should be absolutely fine. Short but complete example:
using System;
public class Test
{
static void Main(string[] args)
{
object o = new object();
Register(o, (s, e) => {});
}
static void Register(object source, EventHandler handler)
{
Console.WriteLine("Handler");
}
static void Register(object source, string text)
{
Console.WriteLine("Text");
}
}
If you could update your question with a similarly short but complete example which doesn't work, we may be able to help further. (My guess is that actually in the process of coming up with a short but complete example, you'll find the problem.)
The fact that your error message explicitly talks about EventHandler
is somewhat suspicious - because the type of your argument isn't EventHandler
, it's just a lambda expression which can be converted to EventHandler
. Are you sure the error is on the calling line?
You need to do:
Register(someObject, new EventHandler((s,e)=>{...}));
Thanks for all the responses... turns out it was a problem in my method declarations. I had the type of the first parameter in my method signature specified wrong. I'll leave this question up for others who make silly mistakes like me.
精彩评论