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

发布于 2024-12-03 22:20:54 字数 2411 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窗影残 2024-12-10 22:20:54

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

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文