【问题标题】:'object' does not contain a definition for dynamic“对象”不包含动态的定义
【发布时间】:2015-11-27 16:29:54
【问题描述】:

我正在使用以下描述方法。即返回动态结果。

public static dynamic GetCouponDetailsbyCouponID(Guid couponID)
        {
            using (var loEntities = new Entities())
            {
                dynamic nonWinnerGift = (from nw in loEntities.CorporateNonWinnerGift
                                         join um in loEntities.Users on nw.UserID equals um.Id
                                         where nw.IsDeleted != true && nw.CouponID == couponID
                                         select new
                                         {
                                             FullName = (um.FirstName + " " + um.LastName),
                                                 Title = nw.Title,
                                                 Description = nw.Description,
                                                 LogoName = nw.LogoName,
                                                 CouponID = nw.CouponID,
                                                 IsDiscount = nw.IsDiscount,
                                                 Discount = nw.Discount,
                                                 Desclaiemer = nw.Desclaiemer
                                             }).SingleOrDefault();    
                return nonWinnerGift;
            }
        }

 dynamic expandDoObject = new ExpandoObject();

当我尝试访问“couponData.LogoName”时,会抛出动态运行时异常。请在下面找到我的例外 “在 ClosetAuctions.dll 中发生了‘Microsoft.CSharp.RuntimeBinder.RuntimeBinderException’类型的第一次机会异常 附加信息:‘object’不包含‘LogoName’的定义”

                var couponData = CorporateNonWinnerGiftBL.GetCouponDetailsbyCouponID(couponID);

                if (couponData != null)
                {
                    string fileName = couponData.LogoName;
                }

【问题讨论】:

  • 为什么要使用动态类型的代码来传递数据?这不是 javascript。
  • 我有两个类,我想通过使用 linq 查询返回组合两个参数。而且我不想创建单独的课程。所以请建议我我需要做什么。
  • 例如,返回一个聚合类。 class Result { public WinnerGift WinnerGift { get; set; } public User User { get; set; } }
  • @decastro,感谢您的快速回复。但是,我不想创建一个新类。如果我通过使用两个类参数创建一个使用新类很容易,但我主要担心的是不想新类。
  • 您正在返回一个匿名类型的实例。如果你没有使用dynamic,你唯一的选择就是返回一个object——匿名类型在你自己的函数之外是未知的。如果你有一个object 类型的变量,你会得到一个编译时错误,它没有LogoName 属性。您对dynamic 所做的只是将完全相同的查找规则推迟到运行时。在运行时,可以确定的最佳类型是object

标签: c# asp.net


【解决方案1】:

不建议在您的用例中使用动态对象。但这是我的看法。

无论如何,要访问动态对象的成员,

string fileName = couponData.GetType().GetProperty("LogoName").GetValue(couponData, null);

【讨论】:

  • 你能解释一下“d”吗? GetValue(d, null)
  • 感谢您的快速回复。我真的很感激,它正在工作。
  • 感谢您的回答,返回列表 很好且易于使用 :)
【解决方案2】:

“RuntimeBinderException”已在以下文章中得到解答,请参考。

https://social.msdn.microsoft.com/Forums/en-US/30b916bf-7e59-4d8d-b7bc-076d4289a018/type-inference-turns-my-vars-to-dynamic?forum=csharplanguage

试试下面的代码:

public static dynamic GetCouponDetailsbyCouponID(Guid couponID)
{
    using (var loEntities = new Entities())
    {
        var nonWinnerGift = (from nw in loEntities.CorporateNonWinnerGift
            join um in loEntities.Users on nw.UserID equals um.Id
            where nw.IsDeleted != true && nw.CouponID == couponID
            select new
            {
                FullName = (um.FirstName + " " + um.LastName),
                Title = nw.Title,
                Description = nw.Description,
                LogoName = nw.LogoName,
                CouponID = nw.CouponID,
                IsDiscount = nw.IsDiscount,
                Discount = nw.Discount,
                Desclaiemer = nw.Desclaiemer

             }).SingleOrDefault();

        dynamic d = new ExpandoObject();

        d.FullName = nonWinnerGift.FullName;
        d.Title = nonWinnerGift.Title;
        d.Description = nonWinnerGift.Description;
        d.LogoName = nonWinnerGift.LogoName;
        d.CouponID = nonWinnerGift.CouponID;
        d.IsDiscount = nonWinnerGift.IsDiscount;
        d.Discount = nonWinnerGift.Discount;
        d.Desclaiemer = nonWinnerGift.Desclaiemer;

        return d;
    }
}

【讨论】:

  • 实际阅读问题和代码的加分项!大声笑很好的答案!
  • 有趣有趣,所以我永远不要忘记不要使用超出其范围的匿名类型,尽管这是我的梦想
猜你喜欢
  • 1970-01-01
  • 2013-08-27
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 2015-09-17
  • 2020-05-05
  • 1970-01-01
相关资源
最近更新 更多