Note that there are some explanatory texts on larger screens.

plurals
  1. POdynamic object - runtime reports field not present but can see it in debugger?
    primarykey
    data
    text
    <p><strong>EDIT : restarting visual studio fixed this issue with no code changes.</strong></p> <hr> <p>I have a ConfigSection handler that uses dynamic types and an expando object. The test fails reporting <strong>'object' does not contain a definition for 'SportName'</strong>. I have tried to replicate in a console taking the ConfigSection handler out of the equation but what looks like the equivalent code works fine. I'm stumped.</p> <p>See below for test, ConfigurationSectionHandler and config xml</p> <pre><code>public class SportSection : IConfigurationSectionHandler { public object Create(object parent, object configContext, XmlNode section) { var doc = XDocument.Parse(section.OuterXml); var root = (XElement)doc.FirstNode; try { var sportList = root.Element("sportList").Elements("sport").Select(ToSport); dynamic config = new ExpandoObject(); config.SportList = sportList; return config; } catch (Exception ex) { throw new ConfigurationErrorsException("Invalid SportSection configuration", ex); } } private static dynamic ToSport(XElement sportElement) { try { var getAttrib = new Func&lt;XElement, string, string&gt;((x, atr) =&gt; x.Attribute(atr).Value); var getDictionary = new Func&lt;IEnumerable&lt;XElement&gt;, IDictionary&lt;string, string&gt;&gt;(elems =&gt; elems.ToDictionary(x =&gt; x.Attribute("name").Value, y =&gt; y.Attribute("value").Value)); return new { SportName = sportElement.Attribute("name").Value, EventProperties = getDictionary(sportElement.Element("eventProperties").Elements("property")), CompetitionProperties = getDictionary(sportElement.Element("competitionProperties").Elements("property")), MappedMarkets = sportElement.Element("mapping").Elements("market").Select(x =&gt; new MappedMarket() { Type = getAttrib(x, "type"), MappedType = getAttrib(x, "mappedType") }) }; } catch (Exception ex) { throw ex; } } } [Test] public void GoodConfig() { var document = new XmlDocument(); document.LoadXml(Resources.ValidSportSectionConfig); var config = new SportSection().Create(null, null, document) as dynamic; IEnumerable&lt;dynamic&gt; sportList = config.SportList; Assert.AreEqual(1, sportList.Count()); //Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'object' does not contain a definition for 'SportName' Assert.AreEqual("Baseball", sportList.Select(x =&gt; (string) x.SportName).First()); var eventProperties = sportList.First(x =&gt; x.SportName == "Baseball").EventProperties as IDictionary&lt;string, string&gt;; Assert.AreEqual(2, eventProperties.Count); Assert.AreEqual("BSB", eventProperties["SportId"]); Assert.AreEqual("THA", eventProperties["CompetitorReferenceId"]); var compProps = sportList.First(x =&gt; x.SportName == "Baseball").CompetitionProperties as IDictionary&lt;string, string&gt;; Assert.AreEqual(2, compProps.Count); Assert.AreEqual("BSB", compProps["SportId"]); Assert.AreEqual("CUP", compProps["CompetitionOrgMethodId"]); var mappedMarkets = (sportList.First(x =&gt; x.SportName == "Baseball").MappedMarkets as IEnumerable&lt;MappedMarket&gt;).ToList(); Assert.AreEqual(2, mappedMarkets.Count()); Assert.AreEqual("match_winner" , mappedMarkets[0].Type); Assert.AreEqual("BSBAO", mappedMarkets[0].MappedType); Assert.AreEqual("handicap", mappedMarkets[0].Type); Assert.AreEqual("BSBAQ", mappedMarkets[0].MappedType); } &lt;sportSettings&gt; &lt;sportList&gt; &lt;sport name="Baseball"&gt; &lt;eventProperties&gt; &lt;property name="SportId" value="BSB"&gt;&lt;/property&gt; &lt;property name="CompetitorReferenceId" value="THA"&gt;&lt;/property&gt; &lt;/eventProperties&gt; &lt;competitionProperties&gt; &lt;property name="SportId" value="BSB" /&gt; &lt;property name="CompetitionOrgMethodId" value="CUP" /&gt; &lt;/competitionProperties&gt; &lt;mapping&gt; &lt;market type="match_winner" mappedType="BSBAO" /&gt; &lt;market type="handicap" mappedType="BSBAQ" /&gt; &lt;/mapping&gt; &lt;/sport&gt; &lt;/sportList&gt; &lt;/sportSettings&gt; </code></pre> <p><strong>UPDATE</strong> - Stack Trace:</p> <pre><code>at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at SS.Integration.EVenue.WindowsService.UnitTests.Configuration.SportSectionTests.&lt;GoodConfig&gt;b__11(Object x) in C:\_Git\SS.Integration.EVenue\SS.Integration.EVenue.WindowsService.UnitTests\Configuration\SportSectionTests.cs:line 35 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload