为什么我在返回动态时收到 RuntimeBinderException?
我正在使用 Massive 并有一个成员表。在我的 Members 类上,我有一个 find 方法来通过电子邮件查找成员:
public class Members : DynamicModel
{
public Members(): base("TSConnection", "Members", "Id"){}
public dynamic Find(string email)
{
dynamic result = new ExpandoObject();
result.Success = false;
try
{
result.Member = this.Single(where: "Email=@0", args: email);
result.Success = result.Member != null;
}
catch (Exception ex)
{
result.Message = ex.Message;
}
return result;
}
}
这会按预期返回所有内容,并且在单独的单元测试中, result.Member.Id
显示它已填充了数据。
在一个单独的类中,地址。我尝试使用此方法来检查用户是否存在并检索要在插入特定成员的地址时使用的 Member.Id:
public class Addresses : DynamicModel
{
public Addresses() : base("TS", "Addresses", "Id") { }
public dynamic Create(string email, string type, string address1, string address2, string city, string state, int countryId, string postcode)
{
var members = new Members();
dynamic result = new ExpandoObject();
result.Success = false;
//var member = members.Find(email);
result.Member = members.Single(where: "Email=@0", args: email);
dynamic address = new ExpandoObject();
if (result.Member != null)
{
address.Id = this.Insert(new {
MemberId = result.Member.Id,
AddressTypeId = (int)AddressType.Account,
Address1 = address1,
Address2 = address2,
City = city,
State = state,
Country = countryId,
Postcode = postcode
});
result.Address = address.Id != 0 ? address : null;
result.Success = address.Id != 0;
}
return result;
}
}
在选项卡式行上,显示 var member = Members.Find(email);
当我尝试访问 member.Member.Id
时,这会返回 RuntimeBinderException。我进入代码,Find 方法“返回”正确的数据,但它没有传递给变量成员。奇怪的是,当我使用代码在下面的行中检索用户时:
result.Member = members.Single(where: "Email=@0", args: email);
它工作正常。但不太适合保持干燥。发生了什么事,有没有办法可以在 Addresses 类中使用 Members 类中的 Find 方法?
劳埃德
I'm using Massive and have a Members table. On my Members class I have a find method to find a member by Email:
public class Members : DynamicModel
{
public Members(): base("TSConnection", "Members", "Id"){}
public dynamic Find(string email)
{
dynamic result = new ExpandoObject();
result.Success = false;
try
{
result.Member = this.Single(where: "Email=@0", args: email);
result.Success = result.Member != null;
}
catch (Exception ex)
{
result.Message = ex.Message;
}
return result;
}
}
This returns everything as expected and in a seperate unit test testing that result.Member.Id
shows it's populated with the data.
In a seperate class, Addresses. I try to use this method to check that the user exists and to retrieve the Member.Id to use when inserting an address for the particular member:
public class Addresses : DynamicModel
{
public Addresses() : base("TS", "Addresses", "Id") { }
public dynamic Create(string email, string type, string address1, string address2, string city, string state, int countryId, string postcode)
{
var members = new Members();
dynamic result = new ExpandoObject();
result.Success = false;
//var member = members.Find(email);
result.Member = members.Single(where: "Email=@0", args: email);
dynamic address = new ExpandoObject();
if (result.Member != null)
{
address.Id = this.Insert(new {
MemberId = result.Member.Id,
AddressTypeId = (int)AddressType.Account,
Address1 = address1,
Address2 = address2,
City = city,
State = state,
Country = countryId,
Postcode = postcode
});
result.Address = address.Id != 0 ? address : null;
result.Success = address.Id != 0;
}
return result;
}
}
On the tabbed out line which reads var member = members.Find(email);
this is returning a RuntimeBinderException when I try to access member.Member.Id
. I step into the code and the Find method is 'returning' the correct data but it's not being passed to the variable member. Oddly when I use the code to retrieve the user on the line below:
result.Member = members.Single(where: "Email=@0", args: email);
It works fine. Not really good for keeping things DRY though. What is going on and is there a way I can use the Find method from my Members class inside my Addresses class?
Lloyd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您从
Find
返回结果
,它是一个ExpandoObject
。要访问Addresses
中的Member
,您需要将代码更改为:It's because you are returning
result
, fromFind
, which is anExpandoObject
. To access theMember
inAddresses
you need to change the code to: