'object' does not contain a definition for dynamic

前端 未结 2 1876
孤街浪徒
孤街浪徒 2021-01-11 17:53

I am using below describe method. That return dynamic result.

public static dynamic GetCouponDetailsbyCouponID(Guid couponID)
        {
            using (v         


        
相关标签:
2条回答
  • 2021-01-11 18:21

    "RuntimeBinderException" has already been answered on below articles please refer it.

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

    Try to below code:

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-11 18:32

    It is not advisable to use dynamic object in your use case. But this is my opinion.

    Anyway, to access member of dynamic object,

    string fileName = couponData.GetType().GetProperty("LogoName").GetValue(couponData, null);
    
    0 讨论(0)
提交回复
热议问题