为什么我在返回动态时遇到RuntimeBinderException?

时间:2011-09-10 14:43:15

标签: asp.net dynamic massive

我正在使用Massive并拥有一个会员表。在我的会员班上,我有一个通过电子邮件找到会员的查找方法:

   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方法?

劳埃德

1 个答案:

答案 0 :(得分:2)

这是因为您从result返回Find,这是ExpandoObject。要访问Member中的Addresses,您需要将代码更改为:

var members = new Members();
var result = members.Find(email);
var member = result.Member;