6

EDIT : restarting visual studio fixed this issue with no code changes.


I have a ConfigSection handler that uses dynamic types and an expando object. The test fails reporting 'object' does not contain a definition for 'SportName'. 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.

See below for test, ConfigurationSectionHandler and config xml

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<XElement, string, string>((x, atr) => x.Attribute(atr).Value);
            var getDictionary =
                new Func<IEnumerable<XElement>, IDictionary<string, string>>(elems => elems.ToDictionary(x => x.Attribute("name").Value, y => 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 => 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<dynamic> 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 => (string) x.SportName).First()); 

        var eventProperties = sportList.First(x => x.SportName == "Baseball").EventProperties as IDictionary<string, string>;

        Assert.AreEqual(2, eventProperties.Count);
        Assert.AreEqual("BSB", eventProperties["SportId"]);
        Assert.AreEqual("THA", eventProperties["CompetitorReferenceId"]);

        var compProps = sportList.First(x => x.SportName == "Baseball").CompetitionProperties as IDictionary<string, string>;
        Assert.AreEqual(2, compProps.Count);
        Assert.AreEqual("BSB", compProps["SportId"]);
        Assert.AreEqual("CUP", compProps["CompetitionOrgMethodId"]);

        var mappedMarkets = (sportList.First(x => x.SportName == "Baseball").MappedMarkets as IEnumerable<MappedMarket>).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);
    }

<sportSettings>
  <sportList>
    <sport name="Baseball">
    <eventProperties>
      <property name="SportId" value="BSB"></property>
      <property name="CompetitorReferenceId" value="THA"></property>
    </eventProperties>
    <competitionProperties>
      <property name="SportId" value="BSB" />
      <property name="CompetitionOrgMethodId" value="CUP" />
    </competitionProperties>
    <mapping>
      <market type="match_winner" mappedType="BSBAO" />
      <market type="handicap" mappedType="BSBAQ" />
    </mapping>
    </sport>
  </sportList>
</sportSettings>

UPDATE - Stack Trace:

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.<GoodConfig>b__11(Object x) in C:\_Git\SS.Integration.EVenue\SS.Integration.EVenue.WindowsService.UnitTests\Configuration\SportSectionTests.cs:line 35
Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
  • 1
    The code looks correct, and you confirm it runs fine in the console. Try debugging the test, and 'tick' handled exceptions. Then start digging at the top of the callstack (show external code) by inspecting the locals. – leppie Apr 25 '12 at 13:16
  • If you mean tick 'Thrown' in Debug > Exception dialog then breaks at same point..have updated with call stack, thx – Myles McDonnell Apr 25 '12 at 13:22
  • 1
    Yes, I mean that. Now when it breaks there, inspect the locals in the top 2 frames. You can also maybe inspect the IL from the assemblies to see if something is amiss. – leppie Apr 25 '12 at 13:25
  • Restarting VS2010 fixed this. – Myles McDonnell Apr 25 '12 at 15:27

1 Answers1

4

ToSport returns an anonymous type rather than an ExpandoObject. You have to be careful when you cast an anonymous type to dynamic, because those types have an access modifier of internal. Thus, if you cross assembly boundaries the runtime won't see any accessable properties. Try:

 private static dynamic ToSport(XElement sportElement)
    {
        try
        {
            var getAttrib = new Func<XElement, string, string>((x, atr) => x.Attribute(atr).Value);
            var getDictionary =
                new Func<IEnumerable<XElement>, IDictionary<string, string>>(elems => elems.ToDictionary(x => x.Attribute("name").Value, y => y.Attribute("value").Value));


            dynamic n = new ExpandoObject();
            n.SportName = sportElement.Attribute("name").Value;
            n.EventProperties = getDictionary(sportElement.Element("eventProperties").Elements("property"));
            n.CompetitionProperties = getDictionary(sportElement.Element("competitionProperties").Elements("property"));
            n.MappedMarkets = sportElement.Element("mapping").Elements("market").Select(x => new MappedMarket() { Type = getAttrib(x, "type"), MappedType = getAttrib(x, "mappedType") });

            return n;
        }
        catch (Exception ex)
        {

            throw ex;
        }

    }
jbtule
  • 31,383
  • 12
  • 95
  • 128
  • That would explain the problem I experienced, although it doesn't explain why restarting VS fixed it. Strange. – Myles McDonnell Apr 27 '12 at 08:39
  • 1
    Diffrent types of builds can sometimes obscure where an assembly boundary is. For example, ASP.NET has pages that are compiled on the fly they can randomly by grouped in to different assemblies. I don't know either why VS fixed it, but i do know it's a really good practice to replace returning anonymous types cast dynamic with ExpandoObjects. – jbtule Apr 27 '12 at 12:27