Skip to content

Instantly share code, notes, and snippets.

@jordansjones
Created June 12, 2013 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordansjones/5762876 to your computer and use it in GitHub Desktop.
Save jordansjones/5762876 to your computer and use it in GitHub Desktop.
Using reflection to call AsyncSubject methods
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