Cannot convert from 'method group' to 'System.Action' error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Cannot convert from 'method group' to 'System.Action<object>' error



I have created the following function:


public void DelegatedCall(Action<Object> delegatedMethod)



And defined the following method


public void foo1(String str) { }



However, when I try to call DelegateCall with foo1:


DelegateCall


foo1


DelegatedCall(foo1);



...I get the following compiler error:



Argument 1: cannot convert from 'method group' to 'System.Action<object>'


Argument 1: cannot convert from 'method group' to 'System.Action<object>'



What is the reason for this error and how can I correct it? Unfortunately, casting foo1 to Action is not an option.


foo1


Action





Related: What is a method group in C#?
– DavidRR
Jun 10 '16 at 12:57




3 Answers
3



DelegatedCall expects a delegate that takes any object as an argument. But your function foo1 that you are passing to DelegatedCall can only cope with a string argument. So, the conversion isn't type-safe and thus is not possible.


DelegatedCall


object


foo1


DelegatedCall


string



Input parameters are contra-variant, but your code needs covariance. (See Difference between Covariance & Contra-variance.)



You can make DelegatedCall generic:


DelegatedCall


DelegatedCall<T>(Action<T> action)



...or have it take any delegate:


DelegatedCall(Delegate action)



But then implementing it is ugly and requires reflection. It also doesn't verify that the function has only one parameter at compile-time.





Are you aware of any way i can create such a code? I want DelegateCall to have the ability to receive a function which receives any one parameter of any type (String, bool, List...)
– Jonathan
Jan 16 '11 at 11:43





Can you make it generic?
– CodesInChaos
Jan 16 '11 at 11:57





Unfortunately no
– Jonathan
Jan 16 '11 at 12:05





Can you recommend on any reading material how to use reflection to implement the second option?
– Jonathan
Jan 16 '11 at 12:10



Variance doesn't work that way around; you would need


DelegatedCall(obj => foo1((string)obj));



As even in 4.0 it won't believe that every object is a string.



Note that if it was foo1(object) and Action<string> (i.e. the other way around) it probably would work (in 4.0), since every string is an object.


foo1(object)


Action<string>





The other way round works even prior to 4.0. When converting from a method group variance was available even in earlier versions.
– CodesInChaos
Jan 16 '11 at 11:35





Ok is there anyway i can make this work then: I want DelegateCall to have the ability to receive a function which receives any one parameter of any type (String, bool, List...). Any help would be appreciated
– Jonathan
Jan 16 '11 at 11:44


private void DeleteStudent(ObservableCollection<User> StudentCollection, User Instance)
{
StudentCollection.Remove(StudentCollection.SingleOrDefault(i => i.UserId == Instance.UserId));
}



Cannot convert from 'method group' to 'System.Action' error



kindly help me to how to convert method to action object???





This does not provide an answer to the question. You can search for similar questions, or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, ask a new question, and include a link to this one to help provide context. See: Ask questions, get answers, no distractions
– Natty
32 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name