c# - With Reactive Extensions (RX) how to solve compile error "cannot convert from 'method group' to 'System.IObserver" -


when using iobservable, line not compile:

var x = receiver.updates().subscribe(onnewmessage); 

i error:

error   3   argument 1: cannot convert 'method group' 'system.iobserver<imyclass>'       

and error:

error   2   best overloaded method match 'system.iobservable<imyclass>.subscribe(system.iobserver<imyclass>)' has invalid arguments     

the source code uses iobservable.

the error

argument 1: cannot convert 'method group' 'system.iobserver<imyclass>'  

is quite clear provided understand terminology. raised because .net framework base class library system.iobservable<t> interface defines single method signature:

idisposable subscribe(system.iobserver<t>) 

and passing method group. method group quite literally says, group of methods. in particular, in case group of methods in scope name onnewmessage. specified passing method name without parentheses. using method group shorthand causes compiler use method overload resolution find method can converted typed delegate. of course, no such method can exist type of system.iobserver<t> not typed delegate.

this error crops lot when forgetting include assembly contains desired extension method and/or forgetting include using statement required namespace of extension method.

in case intention satisfy extension method defined reactive extensions framework - rx defines number of extension methods on iobservable<t> convenient shorthand building observer - implementation of system.iobserver<t> interface - , subscribing observable in 1 go. observer interface defines following 3 methods:

void onnext(t value); void onerror<t>(exception error); void oncompleted(); 

the various extension methods provided rx defined in system.reactive.core , build observer implementation using supplied actions in various combinations fulfill contract above. i've linked actual definition of these in source of rx. simplest 1 accepts onnext handler , provides default implementation onerror , oncompleted methods. looks this:

public static idisposable subscribe<t>(     iobservable<t> source, action<t> onnext); 

these extension methods defined in system namespace, in addition referencing system.reactive.core assembly (in nuget packet rx-core, although users want @ least additional libraries provided rx-main) must have using system; declaration in source file.

the second error:

the best overloaded method match      'system.iobservable<imyclass>.subscribe(system.iobserver<imyclass>)' has invalid arguments. 

is in same vein, confirming best method compiler find (probably method of supplied method group in case) not convertible required type of single iobserver<t> argument subscribe method.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -