C#学习教程:动态对象 – 运行时报告字段不存在但可以在调试器中看到它?分享


动态对象 – 运行时报告字段不存在但可以在调试器中看到它?

编辑:重启visual studio解决了这个问题没有代码更改。


我有一个ConfigSection处理程序,它使用动态类型和一个expando对象。 测试失败报告‘对象’不包含’SportName’的定义 。 我试图在控制台中复制,将ConfigSection处理程序排除在等式之外,但看起来像等效代码的工作正常。 我很难过。

请参阅下面的test,ConfigurationSectionHandler和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((x, atr) => x.Attribute(atr).Value); var getDictionary = new Func<IEnumerable, IDictionary>(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 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; 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; 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).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); }                   

更新 – 堆栈跟踪:

 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.b__11(Object x) in C:_GitSS.Integration.EVenueSS.Integration.EVenue.WindowsService.UnitTestsConfigurationSportSectionTests.cs:line 35 

ToSport返回匿名类型而不是ExpandoObject 。 将匿名类型转换为动态时必须小心,因为这些类型具有internal的访问修饰符。 因此,如果跨越程序集边界,运行时将看不到任何可访问的属性。 尝试:

上述就是C#学习教程:动态对象 – 运行时报告字段不存在但可以在调试器中看到它?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

  private static dynamic ToSport(XElement sportElement) { try { var getAttrib = new Func((x, atr) => x.Attribute(atr).Value); var getDictionary = new Func, IDictionary>(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; } } 

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/959880.html

发表评论

邮箱地址不会被公开。 必填项已用*标注