Created
June 12, 2013 04:39
-
-
Save jordansjones/5762876 to your computer and use it in GitHub Desktop.
Using reflection to call AsyncSubject methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private object InterceptGet(string url, IInvocation invocation) | |
{ | |
var retVal = invocation.Method.ReturnType; | |
if (retVal.IsGenericType && retVal.Name == typeof (IObservable<>).Name) | |
{ | |
retVal = retVal.GenericTypeArguments[0]; | |
} | |
var retType = _subjectType.MakeGenericType(retVal); | |
var ret = Activator.CreateInstance(retType); | |
Observable.Start(() => | |
{ | |
try | |
{ | |
_client.GetAsync(url).ToObservable().Subscribe( | |
x => x.Content.ReadAsStringAsync().ToObservable().Subscribe( | |
s => | |
{ | |
var o = JsonConvert.DeserializeObject(s, retVal); | |
retType.GetMethod("OnNext").Invoke(ret, new[] {o}); | |
retType.GetMethod("OnCompleted", new Type[0]).Invoke(ret, null); | |
} | |
) | |
); | |
} | |
catch (Exception ex) | |
{ | |
retType.GetMethod("OnError", new [] { typeof(Exception)}).Invoke(ret, new [] { ex }); | |
} | |
}, | |
System.Reactive.Concurrency.NewThreadScheduler.Default | |
); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment