Reputation: 47904
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
}
dynamic
? dynamic
?Upvotes: 8
Views: 2954
Reputation: 62246
From the definition in MSDN:
Type dynamic behaves like type object in most circumstances. However, operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time
So dynamic doesn't exist when you compile, cause it needs to convert it into destination *type*, and that's why is not able to resolve it. What is destination type ?
In fact if you do something like this:
static void M(Func<int> f) { }
static void M(Func<string> f) { }
static void M(Func<object> f) { } // could be also declared like dynamic here, works by the way
static object DynamicObject()
{
return new object();
}
static void Test()
{
M(() => 0);
M(() => "");
M(() => DynamicObject());
}
It perfectly works as you want, as object
is present like a type already at compile time, in difference of dynamic
type which have to be converted.
Upvotes: 1
Reputation: 244767
The problem here is type inference. The compiler is trying to find out which overload to use based on the argument, but it's also trying to find out what the type of the argument is based on the chosen overload. In the case of M(() => DynamicObject())
, the process goes something like this:
dynamic
. Because there is an implicit conversion from dynamic
to any other type, we now know all three overloads are good.int
and string
derive from object
, the overloads with int
and string
are considered best.Now, regarding possible solutions to your problem:
Make the type of the lambda explicit, either using cast or typed local variable:
M((Func<dynamic>)(() => DynamicObject()));
or
Func<dynamic> f = () => DynamicObject();
M(f);
Rename the dynamic overload to something like DynamicM
. This way, you don't have to deal with overload resolution.
This one feels somewhat wrong to me: make sure the dynamic
overload is the only one that fits, by casting to object
:
M(() => (object)DynamicObject())
Upvotes: 5