Why would RuntimeBinderException occur at runtime calling an overloaded function?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I've finally produced a minimal example that reproduces this error:



using System;
using Newtonsoft.Json;
class Program
{
public byte Foo(byte p) { return new byte[0]; }
public byte Foo(Guid? p) { return new byte[0]; }
static Guid? ToGuid(string s) { return s == null ? null : (Guid?)new Guid(s); }
void Bar()
{
dynamic d = JsonConvert.DeserializeObject<dynamic>("{}");
var id = d?.id?.ToString();
Foo(ToGuid(id));
}

static void Main(string args)
{
new Program().Bar();
}
}


Bizarrely it's crashing at runtime calling Foo when d.id is null (or not a string), saying it can't resolve which version of Foo to call (The call is ambiguous between the following methods or properties). Why on earth isn't this resolved at compile time though? The dynamic shouldn't make a difference that I can see, and in fact even more weirdly if I add an explicit cast "(Guid?)" before ToGuid... it works as expected, and likewise if I instead write it as:



Guid? id = ToGuid(d.id?.ToString());
Foo(id)


which actually makes more sense anyway. It also works fine if I change "var" to "string".



I noticed that the exception is initially thrown from "System.Linq.Expressions.dll" which is a bit odd. The full stack trace is basically:



Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The call is ambiguous between the following methods or properties: 'FooService.Foo(byte)' and 'FooService.Foo(System.Guid?)'
at CallSite.Target(Closure , CallSite , FooService , Object )



Exception source is "Anonymously Hosted DynamicMethods Assembly"










share|improve this question

























  • Aren't you getting compilation error Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.Guid' at Guid? ToGuid(string s) { return uuid == null ? null : new Guid(uuid) }?

    – Karan
    Jan 3 at 9:35











  • Fixed, sorry...

    – Dylan Nicholson
    Jan 3 at 9:37













  • That's because your example doesn't use dynamic. It only happens if the parameter to ToGuid( ) ultimately comes from a dynamically typed variable.

    – Dylan Nicholson
    Jan 3 at 9:44











  • But further that site seems to use a very old version of C#, it won't even let me write d?.id.ToString() (which seems to be the key part). I'm using 7.1

    – Dylan Nicholson
    Jan 3 at 9:49











  • Can' reproduce. Your code passig in an d with id null or whatever will run perfeclty fine.

    – InBetween
    Jan 3 at 9:59


















0















I've finally produced a minimal example that reproduces this error:



using System;
using Newtonsoft.Json;
class Program
{
public byte Foo(byte p) { return new byte[0]; }
public byte Foo(Guid? p) { return new byte[0]; }
static Guid? ToGuid(string s) { return s == null ? null : (Guid?)new Guid(s); }
void Bar()
{
dynamic d = JsonConvert.DeserializeObject<dynamic>("{}");
var id = d?.id?.ToString();
Foo(ToGuid(id));
}

static void Main(string args)
{
new Program().Bar();
}
}


Bizarrely it's crashing at runtime calling Foo when d.id is null (or not a string), saying it can't resolve which version of Foo to call (The call is ambiguous between the following methods or properties). Why on earth isn't this resolved at compile time though? The dynamic shouldn't make a difference that I can see, and in fact even more weirdly if I add an explicit cast "(Guid?)" before ToGuid... it works as expected, and likewise if I instead write it as:



Guid? id = ToGuid(d.id?.ToString());
Foo(id)


which actually makes more sense anyway. It also works fine if I change "var" to "string".



I noticed that the exception is initially thrown from "System.Linq.Expressions.dll" which is a bit odd. The full stack trace is basically:



Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The call is ambiguous between the following methods or properties: 'FooService.Foo(byte)' and 'FooService.Foo(System.Guid?)'
at CallSite.Target(Closure , CallSite , FooService , Object )



Exception source is "Anonymously Hosted DynamicMethods Assembly"










share|improve this question

























  • Aren't you getting compilation error Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.Guid' at Guid? ToGuid(string s) { return uuid == null ? null : new Guid(uuid) }?

    – Karan
    Jan 3 at 9:35











  • Fixed, sorry...

    – Dylan Nicholson
    Jan 3 at 9:37













  • That's because your example doesn't use dynamic. It only happens if the parameter to ToGuid( ) ultimately comes from a dynamically typed variable.

    – Dylan Nicholson
    Jan 3 at 9:44











  • But further that site seems to use a very old version of C#, it won't even let me write d?.id.ToString() (which seems to be the key part). I'm using 7.1

    – Dylan Nicholson
    Jan 3 at 9:49











  • Can' reproduce. Your code passig in an d with id null or whatever will run perfeclty fine.

    – InBetween
    Jan 3 at 9:59














0












0








0








I've finally produced a minimal example that reproduces this error:



using System;
using Newtonsoft.Json;
class Program
{
public byte Foo(byte p) { return new byte[0]; }
public byte Foo(Guid? p) { return new byte[0]; }
static Guid? ToGuid(string s) { return s == null ? null : (Guid?)new Guid(s); }
void Bar()
{
dynamic d = JsonConvert.DeserializeObject<dynamic>("{}");
var id = d?.id?.ToString();
Foo(ToGuid(id));
}

static void Main(string args)
{
new Program().Bar();
}
}


Bizarrely it's crashing at runtime calling Foo when d.id is null (or not a string), saying it can't resolve which version of Foo to call (The call is ambiguous between the following methods or properties). Why on earth isn't this resolved at compile time though? The dynamic shouldn't make a difference that I can see, and in fact even more weirdly if I add an explicit cast "(Guid?)" before ToGuid... it works as expected, and likewise if I instead write it as:



Guid? id = ToGuid(d.id?.ToString());
Foo(id)


which actually makes more sense anyway. It also works fine if I change "var" to "string".



I noticed that the exception is initially thrown from "System.Linq.Expressions.dll" which is a bit odd. The full stack trace is basically:



Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The call is ambiguous between the following methods or properties: 'FooService.Foo(byte)' and 'FooService.Foo(System.Guid?)'
at CallSite.Target(Closure , CallSite , FooService , Object )



Exception source is "Anonymously Hosted DynamicMethods Assembly"










share|improve this question
















I've finally produced a minimal example that reproduces this error:



using System;
using Newtonsoft.Json;
class Program
{
public byte Foo(byte p) { return new byte[0]; }
public byte Foo(Guid? p) { return new byte[0]; }
static Guid? ToGuid(string s) { return s == null ? null : (Guid?)new Guid(s); }
void Bar()
{
dynamic d = JsonConvert.DeserializeObject<dynamic>("{}");
var id = d?.id?.ToString();
Foo(ToGuid(id));
}

static void Main(string args)
{
new Program().Bar();
}
}


Bizarrely it's crashing at runtime calling Foo when d.id is null (or not a string), saying it can't resolve which version of Foo to call (The call is ambiguous between the following methods or properties). Why on earth isn't this resolved at compile time though? The dynamic shouldn't make a difference that I can see, and in fact even more weirdly if I add an explicit cast "(Guid?)" before ToGuid... it works as expected, and likewise if I instead write it as:



Guid? id = ToGuid(d.id?.ToString());
Foo(id)


which actually makes more sense anyway. It also works fine if I change "var" to "string".



I noticed that the exception is initially thrown from "System.Linq.Expressions.dll" which is a bit odd. The full stack trace is basically:



Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The call is ambiguous between the following methods or properties: 'FooService.Foo(byte)' and 'FooService.Foo(System.Guid?)'
at CallSite.Target(Closure , CallSite , FooService , Object )



Exception source is "Anonymously Hosted DynamicMethods Assembly"







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 11:26







Dylan Nicholson

















asked Jan 3 at 9:23









Dylan NicholsonDylan Nicholson

940517




940517













  • Aren't you getting compilation error Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.Guid' at Guid? ToGuid(string s) { return uuid == null ? null : new Guid(uuid) }?

    – Karan
    Jan 3 at 9:35











  • Fixed, sorry...

    – Dylan Nicholson
    Jan 3 at 9:37













  • That's because your example doesn't use dynamic. It only happens if the parameter to ToGuid( ) ultimately comes from a dynamically typed variable.

    – Dylan Nicholson
    Jan 3 at 9:44











  • But further that site seems to use a very old version of C#, it won't even let me write d?.id.ToString() (which seems to be the key part). I'm using 7.1

    – Dylan Nicholson
    Jan 3 at 9:49











  • Can' reproduce. Your code passig in an d with id null or whatever will run perfeclty fine.

    – InBetween
    Jan 3 at 9:59



















  • Aren't you getting compilation error Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.Guid' at Guid? ToGuid(string s) { return uuid == null ? null : new Guid(uuid) }?

    – Karan
    Jan 3 at 9:35











  • Fixed, sorry...

    – Dylan Nicholson
    Jan 3 at 9:37













  • That's because your example doesn't use dynamic. It only happens if the parameter to ToGuid( ) ultimately comes from a dynamically typed variable.

    – Dylan Nicholson
    Jan 3 at 9:44











  • But further that site seems to use a very old version of C#, it won't even let me write d?.id.ToString() (which seems to be the key part). I'm using 7.1

    – Dylan Nicholson
    Jan 3 at 9:49











  • Can' reproduce. Your code passig in an d with id null or whatever will run perfeclty fine.

    – InBetween
    Jan 3 at 9:59

















Aren't you getting compilation error Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.Guid' at Guid? ToGuid(string s) { return uuid == null ? null : new Guid(uuid) }?

– Karan
Jan 3 at 9:35





Aren't you getting compilation error Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.Guid' at Guid? ToGuid(string s) { return uuid == null ? null : new Guid(uuid) }?

– Karan
Jan 3 at 9:35













Fixed, sorry...

– Dylan Nicholson
Jan 3 at 9:37







Fixed, sorry...

– Dylan Nicholson
Jan 3 at 9:37















That's because your example doesn't use dynamic. It only happens if the parameter to ToGuid( ) ultimately comes from a dynamically typed variable.

– Dylan Nicholson
Jan 3 at 9:44





That's because your example doesn't use dynamic. It only happens if the parameter to ToGuid( ) ultimately comes from a dynamically typed variable.

– Dylan Nicholson
Jan 3 at 9:44













But further that site seems to use a very old version of C#, it won't even let me write d?.id.ToString() (which seems to be the key part). I'm using 7.1

– Dylan Nicholson
Jan 3 at 9:49





But further that site seems to use a very old version of C#, it won't even let me write d?.id.ToString() (which seems to be the key part). I'm using 7.1

– Dylan Nicholson
Jan 3 at 9:49













Can' reproduce. Your code passig in an d with id null or whatever will run perfeclty fine.

– InBetween
Jan 3 at 9:59





Can' reproduce. Your code passig in an d with id null or whatever will run perfeclty fine.

– InBetween
Jan 3 at 9:59












1 Answer
1






active

oldest

votes


















0














Now that we have the var variant I can reproduce the issue. And the issue is nulls. You may think that the return type of ToGuid must be a Guid? because you're assuming knowledge that the compiler doesn't work with. So far as it's concerned, in Bar it's looking at an id with type dynamic1. This means that it's going to assume that whatever ToGuid returns it'll store in a dynamic temp variable.



In this case, it returns null and under the covers, dynamic is just object. At that point, we've lost any compile time type information about the return type from ToGuid. If it wasn't null, before it resolves Foo it would effectively call GetType on the instance. But that's not possible here. It has null and two equally good/bad candidates it could call with a null reference. It's as if you'd written Foo(null); (which generates the equivalent error at compile time).



Inserting an explicit cast - Foo((Guid?)ToGuid(id)); gives the runtime sufficient information at the callsite to be able to unambiguously select the correct overload of Foo you wanted it to choose.





1Bear in mind that whatever the type of the id property on d is, it may have a ToString method that shadows the one from object. It cannot assume it returns a string so id is dynamic too.






share|improve this answer


























  • But at compile time there's only one possible return type from ToGuid (regardless of the parameter type) - so I'm still confused as to why it doesn't resolve the ambiguity at compile time.

    – Dylan Nicholson
    Jan 3 at 11:28











  • Actually it doesn't need the Newtonsoft type either, just void Bar() { dynamic id = null; Foo(ToGuid(id)); } is enough. And yes I understand (now!) that id was of type dynamic. But that doesn't properly explain why ToGuid(id) is also of type dynamic.

    – Dylan Nicholson
    Jan 3 at 11:31













  • @DylanNicholson - because the whole point of dynamic is to defer these sorts of decisions until runtime. The only expressions that use dynamic variables and don't assume the type of that expression is also dynamic are explicit casts and constructor calls.

    – Damien_The_Unbeliever
    Jan 3 at 11:31













  • Well I've ticked your answer even if I'm not particularly satisfied it makes sense for the compiler to behave this way.

    – Dylan Nicholson
    Jan 3 at 11:33











  • Also I've now realised in my current code it's unnecessarily calling ToGuid via run-time binding too.

    – Dylan Nicholson
    Jan 3 at 11:42












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019407%2fwhy-would-runtimebinderexception-occur-at-runtime-calling-an-overloaded-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Now that we have the var variant I can reproduce the issue. And the issue is nulls. You may think that the return type of ToGuid must be a Guid? because you're assuming knowledge that the compiler doesn't work with. So far as it's concerned, in Bar it's looking at an id with type dynamic1. This means that it's going to assume that whatever ToGuid returns it'll store in a dynamic temp variable.



In this case, it returns null and under the covers, dynamic is just object. At that point, we've lost any compile time type information about the return type from ToGuid. If it wasn't null, before it resolves Foo it would effectively call GetType on the instance. But that's not possible here. It has null and two equally good/bad candidates it could call with a null reference. It's as if you'd written Foo(null); (which generates the equivalent error at compile time).



Inserting an explicit cast - Foo((Guid?)ToGuid(id)); gives the runtime sufficient information at the callsite to be able to unambiguously select the correct overload of Foo you wanted it to choose.





1Bear in mind that whatever the type of the id property on d is, it may have a ToString method that shadows the one from object. It cannot assume it returns a string so id is dynamic too.






share|improve this answer


























  • But at compile time there's only one possible return type from ToGuid (regardless of the parameter type) - so I'm still confused as to why it doesn't resolve the ambiguity at compile time.

    – Dylan Nicholson
    Jan 3 at 11:28











  • Actually it doesn't need the Newtonsoft type either, just void Bar() { dynamic id = null; Foo(ToGuid(id)); } is enough. And yes I understand (now!) that id was of type dynamic. But that doesn't properly explain why ToGuid(id) is also of type dynamic.

    – Dylan Nicholson
    Jan 3 at 11:31













  • @DylanNicholson - because the whole point of dynamic is to defer these sorts of decisions until runtime. The only expressions that use dynamic variables and don't assume the type of that expression is also dynamic are explicit casts and constructor calls.

    – Damien_The_Unbeliever
    Jan 3 at 11:31













  • Well I've ticked your answer even if I'm not particularly satisfied it makes sense for the compiler to behave this way.

    – Dylan Nicholson
    Jan 3 at 11:33











  • Also I've now realised in my current code it's unnecessarily calling ToGuid via run-time binding too.

    – Dylan Nicholson
    Jan 3 at 11:42
















0














Now that we have the var variant I can reproduce the issue. And the issue is nulls. You may think that the return type of ToGuid must be a Guid? because you're assuming knowledge that the compiler doesn't work with. So far as it's concerned, in Bar it's looking at an id with type dynamic1. This means that it's going to assume that whatever ToGuid returns it'll store in a dynamic temp variable.



In this case, it returns null and under the covers, dynamic is just object. At that point, we've lost any compile time type information about the return type from ToGuid. If it wasn't null, before it resolves Foo it would effectively call GetType on the instance. But that's not possible here. It has null and two equally good/bad candidates it could call with a null reference. It's as if you'd written Foo(null); (which generates the equivalent error at compile time).



Inserting an explicit cast - Foo((Guid?)ToGuid(id)); gives the runtime sufficient information at the callsite to be able to unambiguously select the correct overload of Foo you wanted it to choose.





1Bear in mind that whatever the type of the id property on d is, it may have a ToString method that shadows the one from object. It cannot assume it returns a string so id is dynamic too.






share|improve this answer


























  • But at compile time there's only one possible return type from ToGuid (regardless of the parameter type) - so I'm still confused as to why it doesn't resolve the ambiguity at compile time.

    – Dylan Nicholson
    Jan 3 at 11:28











  • Actually it doesn't need the Newtonsoft type either, just void Bar() { dynamic id = null; Foo(ToGuid(id)); } is enough. And yes I understand (now!) that id was of type dynamic. But that doesn't properly explain why ToGuid(id) is also of type dynamic.

    – Dylan Nicholson
    Jan 3 at 11:31













  • @DylanNicholson - because the whole point of dynamic is to defer these sorts of decisions until runtime. The only expressions that use dynamic variables and don't assume the type of that expression is also dynamic are explicit casts and constructor calls.

    – Damien_The_Unbeliever
    Jan 3 at 11:31













  • Well I've ticked your answer even if I'm not particularly satisfied it makes sense for the compiler to behave this way.

    – Dylan Nicholson
    Jan 3 at 11:33











  • Also I've now realised in my current code it's unnecessarily calling ToGuid via run-time binding too.

    – Dylan Nicholson
    Jan 3 at 11:42














0












0








0







Now that we have the var variant I can reproduce the issue. And the issue is nulls. You may think that the return type of ToGuid must be a Guid? because you're assuming knowledge that the compiler doesn't work with. So far as it's concerned, in Bar it's looking at an id with type dynamic1. This means that it's going to assume that whatever ToGuid returns it'll store in a dynamic temp variable.



In this case, it returns null and under the covers, dynamic is just object. At that point, we've lost any compile time type information about the return type from ToGuid. If it wasn't null, before it resolves Foo it would effectively call GetType on the instance. But that's not possible here. It has null and two equally good/bad candidates it could call with a null reference. It's as if you'd written Foo(null); (which generates the equivalent error at compile time).



Inserting an explicit cast - Foo((Guid?)ToGuid(id)); gives the runtime sufficient information at the callsite to be able to unambiguously select the correct overload of Foo you wanted it to choose.





1Bear in mind that whatever the type of the id property on d is, it may have a ToString method that shadows the one from object. It cannot assume it returns a string so id is dynamic too.






share|improve this answer















Now that we have the var variant I can reproduce the issue. And the issue is nulls. You may think that the return type of ToGuid must be a Guid? because you're assuming knowledge that the compiler doesn't work with. So far as it's concerned, in Bar it's looking at an id with type dynamic1. This means that it's going to assume that whatever ToGuid returns it'll store in a dynamic temp variable.



In this case, it returns null and under the covers, dynamic is just object. At that point, we've lost any compile time type information about the return type from ToGuid. If it wasn't null, before it resolves Foo it would effectively call GetType on the instance. But that's not possible here. It has null and two equally good/bad candidates it could call with a null reference. It's as if you'd written Foo(null); (which generates the equivalent error at compile time).



Inserting an explicit cast - Foo((Guid?)ToGuid(id)); gives the runtime sufficient information at the callsite to be able to unambiguously select the correct overload of Foo you wanted it to choose.





1Bear in mind that whatever the type of the id property on d is, it may have a ToString method that shadows the one from object. It cannot assume it returns a string so id is dynamic too.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 11:29

























answered Jan 3 at 11:26









Damien_The_UnbelieverDamien_The_Unbeliever

198k17256347




198k17256347













  • But at compile time there's only one possible return type from ToGuid (regardless of the parameter type) - so I'm still confused as to why it doesn't resolve the ambiguity at compile time.

    – Dylan Nicholson
    Jan 3 at 11:28











  • Actually it doesn't need the Newtonsoft type either, just void Bar() { dynamic id = null; Foo(ToGuid(id)); } is enough. And yes I understand (now!) that id was of type dynamic. But that doesn't properly explain why ToGuid(id) is also of type dynamic.

    – Dylan Nicholson
    Jan 3 at 11:31













  • @DylanNicholson - because the whole point of dynamic is to defer these sorts of decisions until runtime. The only expressions that use dynamic variables and don't assume the type of that expression is also dynamic are explicit casts and constructor calls.

    – Damien_The_Unbeliever
    Jan 3 at 11:31













  • Well I've ticked your answer even if I'm not particularly satisfied it makes sense for the compiler to behave this way.

    – Dylan Nicholson
    Jan 3 at 11:33











  • Also I've now realised in my current code it's unnecessarily calling ToGuid via run-time binding too.

    – Dylan Nicholson
    Jan 3 at 11:42



















  • But at compile time there's only one possible return type from ToGuid (regardless of the parameter type) - so I'm still confused as to why it doesn't resolve the ambiguity at compile time.

    – Dylan Nicholson
    Jan 3 at 11:28











  • Actually it doesn't need the Newtonsoft type either, just void Bar() { dynamic id = null; Foo(ToGuid(id)); } is enough. And yes I understand (now!) that id was of type dynamic. But that doesn't properly explain why ToGuid(id) is also of type dynamic.

    – Dylan Nicholson
    Jan 3 at 11:31













  • @DylanNicholson - because the whole point of dynamic is to defer these sorts of decisions until runtime. The only expressions that use dynamic variables and don't assume the type of that expression is also dynamic are explicit casts and constructor calls.

    – Damien_The_Unbeliever
    Jan 3 at 11:31













  • Well I've ticked your answer even if I'm not particularly satisfied it makes sense for the compiler to behave this way.

    – Dylan Nicholson
    Jan 3 at 11:33











  • Also I've now realised in my current code it's unnecessarily calling ToGuid via run-time binding too.

    – Dylan Nicholson
    Jan 3 at 11:42

















But at compile time there's only one possible return type from ToGuid (regardless of the parameter type) - so I'm still confused as to why it doesn't resolve the ambiguity at compile time.

– Dylan Nicholson
Jan 3 at 11:28





But at compile time there's only one possible return type from ToGuid (regardless of the parameter type) - so I'm still confused as to why it doesn't resolve the ambiguity at compile time.

– Dylan Nicholson
Jan 3 at 11:28













Actually it doesn't need the Newtonsoft type either, just void Bar() { dynamic id = null; Foo(ToGuid(id)); } is enough. And yes I understand (now!) that id was of type dynamic. But that doesn't properly explain why ToGuid(id) is also of type dynamic.

– Dylan Nicholson
Jan 3 at 11:31







Actually it doesn't need the Newtonsoft type either, just void Bar() { dynamic id = null; Foo(ToGuid(id)); } is enough. And yes I understand (now!) that id was of type dynamic. But that doesn't properly explain why ToGuid(id) is also of type dynamic.

– Dylan Nicholson
Jan 3 at 11:31















@DylanNicholson - because the whole point of dynamic is to defer these sorts of decisions until runtime. The only expressions that use dynamic variables and don't assume the type of that expression is also dynamic are explicit casts and constructor calls.

– Damien_The_Unbeliever
Jan 3 at 11:31







@DylanNicholson - because the whole point of dynamic is to defer these sorts of decisions until runtime. The only expressions that use dynamic variables and don't assume the type of that expression is also dynamic are explicit casts and constructor calls.

– Damien_The_Unbeliever
Jan 3 at 11:31















Well I've ticked your answer even if I'm not particularly satisfied it makes sense for the compiler to behave this way.

– Dylan Nicholson
Jan 3 at 11:33





Well I've ticked your answer even if I'm not particularly satisfied it makes sense for the compiler to behave this way.

– Dylan Nicholson
Jan 3 at 11:33













Also I've now realised in my current code it's unnecessarily calling ToGuid via run-time binding too.

– Dylan Nicholson
Jan 3 at 11:42





Also I've now realised in my current code it's unnecessarily calling ToGuid via run-time binding too.

– Dylan Nicholson
Jan 3 at 11:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019407%2fwhy-would-runtimebinderexception-occur-at-runtime-calling-an-overloaded-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory