I am using below describe method. That return 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 am trying to access “couponData.LogoName” than thrown dynamic run-time exception. Please find below my exception
“A first chance exception of type ‘Microsoft.CSharp.RuntimeBinder.RuntimeBinderException’ occurred in ClosetAuctions.dll
Additional information: ‘object’ does not contain a definition for ‘LogoName’”
var couponData = CorporateNonWinnerGiftBL.GetCouponDetailsbyCouponID(couponID);
if (couponData != null)
{
string fileName = couponData.LogoName;
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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);
Method 2
“RuntimeBinderException” has already been answered on below articles please refer it.
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;
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0