This may have been answered before. I see many "dynamic method overload resolution" questions, but none that deal specifically with passing a dynamic argument. In the following code, in Test, the last call to M cannot be resolved (it doesn't compile). The error is: the call is ambiguous between [the first two overloads of M].
static void M(Func<int> f) { }
static void M(Func<string> f) { }
static void M(Func<dynamic> f) { }
static dynamic DynamicObject() {
return new object();
}
static void Test() {
M(() => 0);
M(() => "");
M(() => DynamicObject()); //doesn't compile
}
- Why, since the type isn't statically known, does it not resolve to the overload accepting
dynamic? - Is it even possible for an overloaded method to use
dynamic? - What is the best way to resolve this?
static void M<T>(Func<DataTime, T> f) where T : Aandstatic void M<T>(Func<DataTime, T> f) where T : Band I think that would allow the overloading? - Certainly all of the method calls inTestshould be ok?dynamicto an overloaded method. How is that best resolved?object. See Tigran's working example.