"object" does not contain a definition for dynamic

I use the descriptive method below. This returns a dynamic result.

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(); 

When I try to access "couponData.LogoName" than a dynamic exception exception at runtime. Below you will find my exception "First closing exception type" Microsoft.CSharp.RuntimeBinder.RuntimeBinderException "occurred in ClosetAuctions.dll enter image description here Additional Information: 'object' does not contain a definition for 'LogoName' "

  var couponData = CorporateNonWinnerGiftBL.GetCouponDetailsbyCouponID(couponID); if (couponData != null) { string fileName = couponData.LogoName; } 
+6
source share
2 answers

"RuntimeBinderException" has already been answered in the following articles, please refer to it.

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

Try it below: open static dynamic method 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; } } 
+2
source

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

In any case, to access a member of a dynamic object,

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

All Articles