'object'不包含动态定义

时间:2022-01-04 16:53:32

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 'object'不包含动态定义Additional information: 'object' does not contain a definition for 'LogoName'"

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

                var couponData = CorporateNonWinnerGiftBL.GetCouponDetailsbyCouponID(couponID);

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

2 个解决方案

#1


3  

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

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

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) {

尝试下面的代码: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;
        }
    }

#2


5  

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

#1


3  

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

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

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) {

尝试下面的代码: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;
        }
    }

#2


5  

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