locked
RuntimeBinder Exception when binding dynamic items to object properties

    Question

  • The following code when used in an alternate solution worked fine, but does not work for this. The only thing that changed was the UI, but I don't see how that has any effect on this code.

    dynamic resultObj = JArray.Parse(finalString);
    
                        foreach (dynamic item in resultObj)
                        {
                            Promotion branchObj = new Promotion()
                            {
                                sCategory = item.sCategory,
                                sExpireDate = item.sExpireDate,
                                sPromoDetails = item.sPromoDetails,
                                sPromoMain = item.sPromoMain,
                                sPromoUrl = item.sPromoUrl,
                            };
    
                            promoItems.Add(branchObj);
    }

    the detailed error message is as follows:

    'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'sCategory'

    Any help would be much appreciated...

    Tuesday, November 11, 2014 12:08 PM

Answers

  • Hi S_Gerard,

    It seems that you code are correct. I have seen a scenario would cause this error. If one machine had another version of Json.NET installed in GAC, it would produce this kind of error. Please check this on your machine, if you can see Json.NET in GAC, clear the assembly and try your app again.

    If you still have concerns, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, November 21, 2014 8:19 AM
    Moderator

All replies

  • Hello,

    I guess, your final string does not represent an array of objects which have an "sCategory" property.
    For which value of finalString the exception is thrown? This helps us to reproduce the problem.

    Regards,
    Henning Dieterichs
    Developer-Hotline for MSDN Online Germany

    Disclaimer:
    Please take into consideration, that further inquiries cannot or will be answered with delay.
    For further information please contact us per telephone through the MSDN-Entwickler-Hotline: http://www.msdn-online.de/Hotline
    MSDN-Entwickler-Hotline: Fast and professional help for software developers free of charge!

    For this post by the MSDN-Entwickler-Hotline the following terms and conditions apply: Trademarks, Privacy as well as the separate terms of use for the MSDN-Entwickler-Hotline .

    Tuesday, November 11, 2014 1:03 PM
  • Hi,

    the exception is thrown for all the values, not just sCategory.

    finalString does represent an array, I checked this with a breakpoint there. I'm not able to upload an image due to forum restrictions. I'm assuming this problem is due to values of dynamic types in 'item' not being exposed to the client assembly at runtime? but it works fine in an alternate solution.

    Wednesday, November 12, 2014 5:38 AM
  • Hi S_Gerard,

    It seems that you code are correct. I have seen a scenario would cause this error. If one machine had another version of Json.NET installed in GAC, it would produce this kind of error. Please check this on your machine, if you can see Json.NET in GAC, clear the assembly and try your app again.

    If you still have concerns, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, November 21, 2014 8:19 AM
    Moderator
  • Thanks, I hadn't thought of that. I will try and let you know.

    Thanks you :)

    Friday, November 21, 2014 11:10 AM