我遇到错误
无法对空引用执行运行时绑定(bind)
说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
异常详细信息:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法对空引用执行运行时绑定(bind)
源错误:
第27行:lblBirthday.Text =(myInfo.birthday == null?string.Empty:DateTime.Parse(myInfo.birthday).ToString(“dd-MMM-yy”)));
第28行:lblHometown.Text =(myInfo.hometown.name == null?string.Empty:myInfo.hometown.name);
第29行:lblLocation.Text =(myInfo.location.name == null?string.Empty:myInfo.location.name);
第30行:pnlHello.Visible = true;
第31行:}
这是我的代码:
var fb = new FacebookWebClient();
dynamic myInfo = fb.Get("me");
lblName.Text = myInfo.name;
imgProfile.ImageUrl = "https://graph.facebook.com/" + myInfo.id + "/picture";
lblBirthday.Text = (myInfo.birthday == null ? string.Empty : DateTime.Parse(myInfo.birthday).ToString("dd-MMM-yy"));
lblHometown.Text = (myInfo.hometown.name == null ? string.Empty : myInfo.hometown.name);
lblLocation.Text = (myInfo.location.name == null ? string.Empty : myInfo.location.name);
pnlHello.Visible = true;
最佳答案
首先检查myInfo.location
是否为空:
lblLocation.Text = myInfo.location == null ? "" : myInfo.location.name ?? "";
(对于类似的成员也是如此。)
诚然,这有点麻烦,但是基本上,您需要考虑可能为空的任何内容,以确保您不尝试取消引用它。
关于c# - 错误: Unable to perform runtime binding (when location/hometown/any setting is not set in facebook profile),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5336163/