-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
When you use dynamic incorrectly, the resulting RuntimeBinderException can be fairly confusing. Can the situation be improved?
For example, consider this code (based on code from a confused user from dotnet/docs#4487):
dynamic fileContent = 42;
new System.Net.Http.StringContent(fileContent, Encoding.UTF8, "application/base64");This throws:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for
'System.Net.Http.StringContent.StringContent(string, System.Text.Encoding, string)' has some
invalid arguments
at CallSite.Target(Closure , CallSite , Type , Object , Encoding , String )
at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0
arg0, T1 arg1, T2 arg2, T3 arg3)
at UserQuery.RunUserAuthoredQuery()
To somebody who does not know how dynamic is implemented, this exception is going to be very confusing, because:
- There is almost no indication that this exception is related to
dynamic(except for theSystem.Dynamicnamespace in the stack trace). - There is no indication regarding which arguments are invalid.
- Nitpick: The error message talks about overloaded method, when it's actually a constructor.
Since the exception type can't really be changed because of backwards compatibility, that leaves the exception message. Maybe it could be changed to something like the following?
Error in dynamic binding: The best overloaded constructor match for 'System.Net.Http.StringContent.StringContent(string, System.Text.Encoding, string)' has some invalid arguments:
Argument 1: cannot convert from 'int' to 'string'
Do you agree that this is something that should be improved? Is changing the error message to something like the above the way to go?