2

The method I would like to obtain is Observable.Return from System.Reactive.

It's defined like this:

public static IObservable<TResult> Return<TResult>(TResult value)
{
    ...
}

I've tried with

Type observableType = typeof(Observable);
MethodInfo returnMethodInfo = observableType.GetMethod("Return");
var genericMethodInfo = returnMethodInfo.MakeGenericMethod(typeof(int));

The problem is that I'm getting an error message:

Ambiguous match found.

I guess it's because there is another method called "Return":

public static IObservable<TResult> Return<TResult>(TResult value, IScheduler scheduler)
{
   ...
}

How should I call GetMethod to get a MethodInfo to the method I want?

SuperJMN
  • 13,110
  • 16
  • 86
  • 185

2 Answers2

2

You could try it with this solution.

    var method = typeof(Observable).GetMethods().FirstOrDefault(
            x => x.Name.Equals("Return", StringComparison.OrdinalIgnoreCase) &&
                x.IsGenericMethod && x.GetParameters().Length == 1)
        ?.MakeGenericMethod(typeof(int));
MrSpt
  • 499
  • 3
  • 16
1

You'll need to use GetMethods() and filter to the one you want; this could be as simple as:

var returnMethodInfo = observableType.GetMethods().Single(x => x.Name == "Return"
    && x.IsGenericMethodDefinition && x.GetParameters().Length == 1);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900