Reputation: 3944
I'm writing a class that implements 3rd party interface. One of the functions of the interface accepts a parameter of type "object".
Within the function, I need to call a function that I want to pass in to the function as the object parameter, but I can't get it to work.
Example:
public class ABC : Ixyz
{
...
public void DoSomething(object parameter);
{
// Here I will execute the function passed in
}
...
}
public void MySomething()
{
...
}
public void SomeFunction()
{
ABC myABCobject = new ABC();
myABCobject.DoSomething(MySomething);
}
When I try to compile this, I get the following error:
Cannot convert method group 'MySomething' to non-delegate type 'object'. Did you intend to invoke the method?
I have the feeling that I should be doing something with delegates, but since I'm implementing a 3rd party interface, I can't change the signature of the function.
Can I somehow pass in a reference to this function that's expecting an "object"? I would think that I could, since Delegate inherits from Object.
Upvotes: 3
Views: 2264
Reputation: 5194
try casting your parameter to the call to DoSomething as an action - e.g.
myABCobject.DoSomething((Action)MySomething);
I'm pretty sure that'll work...
in fact I just tested it and it does - you can cast the action back in the DoSomething method and invoke it - e.g.
var newparameter = (Action) parameter;
newparameter();
Upvotes: 4