开发者

Why am I getting a RuntimeBinderException when returning dynamic?

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", "Member开发者_JS百科s", "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


It's because you are returning result, from Find, which is an ExpandoObject. To access the Member in Addresses you need to change the code to:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜