- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
编辑:重新启动 visual studio 解决了这个问题,没有更改代码。
我有一个使用动态类型和 expando 对象的 ConfigSection 处理程序。测试失败报告 'object' 不包含 'SportName' 的定义。我试图在控制台中复制,将 ConfigSection 处理程序排除在等式之外,但看起来等效代码工作正常。我很难过。
请参阅下面的测试、ConfigurationSectionHandler 和配置 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>
更新 - 堆栈跟踪:
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
最佳答案
ToSport
返回匿名类型而不是 ExpandoObject
。将匿名类型转换为动态类型时必须小心,因为这些类型具有 internal
的访问修饰符。因此,如果您跨越程序集边界,运行时将看不到任何可访问的属性。尝试:
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;
}
}
关于c# - 动态对象 - 运行时报告字段不存在但可以在调试器中看到它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10316344/
我有一个可用于开发但不适用于服务器的 vbscript。 我想调试这个,但我不想在服务器上安装visual studio。 使用调试器进行调试的最轻量级方法是什么? 最佳答案 如果您指的是“经典”VB
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
我的公司有一个使用嵌入在其运行时中的 Lua 的程序,正在加载 .lua磁盘中的文件并重复执行其中定义的功能。 有没有办法附加到正在运行的进程并在我的 .lua 中设置断点?文件? (我会接受 gdb
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
在使用 perl 调试器时,有没有办法跳出当前循环? 例如: line 1 for($i=1;$iperl -d Loading DB routines from perl5db.pl version
我有一个递归下降树对象。我希望能够设置断点并在 Xcode 调试器中检查它。检查顶层工作得很好。但是在我下降一个级别后,调试器说 ivars 的值超出了范围。有什么办法可以防止这种情况发生吗? 编辑:
我正在用 javascript for windows(以及在 wsf 中使用 javascript 和 vbscript)编写桌面脚本,而不是用于 internet 并且不使用任何资源管理器。我需要
我在测试模块的新添加时遇到了问题。 (特别是 - ~ 运算符似乎仅在 Math::Complex 中不适用于此新功能。)它看起来太奇怪了,但理想的方案是在 . t 程序。 好吧,我很快就放弃了那个的想
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
谁能告诉我可用于 C++ 语言的调试器有哪些。另请提供有关这些调试器的详细信息或引用以获取相同的详细信息。 最佳答案 Wikipedia有一个完整的调试器列表。比任何个人都大得多,会从他们的头顶发出嘎
要在 Python 脚本中添加临时调试器断点,我可以插入以下行 import pdb; pdb.set_trace() Pdb 从标准输入中读取,因此如果脚本本身也从标准输入中读取,这将不起作用。作为
我想设置一些调试命令(如 import ipdb; ipdb.set_trace()),以便在 jupyter 中运行调试器(我必须运行 HTTP 服务器)。有人知道这样的事情吗? 上下文:我有一个长
我发现可以使用以下代码从代码中调用 pdb 调试器: import pdb; pdb.set_trace() 是否有 Pycharm 调试器的等效项?因为我更愿意只学习一个调试器。我想运行,而不
那里有免费的 LINQ 调试器吗?我在 LINQ 方面很糟糕,我要改进的唯一方法就是拥有一些简单的调试器,我可以在其中调试我用它犯的愚蠢错误。 有什么建议吗?一个简单的谷歌查询网络 bupkis 免费
我开始使用 realgud在 GNU Emacs 24.3.1 中作为 GUD 的替代品。 (主要是因为当我用 vanilla pdb 设置断点时,它不尊重它。) 我正在使用 pdb bin/star
考虑这个小的 perl 程序,test.pl : #!/usr/bin/env perl use warnings; use strict; use Number::Format qw(:subs);
有没有办法用其他应用程序(例如 Eclipse)控制富士通 Softune 调试器?我考虑发送 Softune 文档中提到的命令并解析输出,但也欢迎其他方法。 最佳答案 eclipse有插件;文件名为
我正在开发一个需要网络登录的 iPhone 应用程序。像往常一样我打电话 [[UIApplication sharedApplication] openURL:loginURL]; 这将关闭应用程序并
我目前正在研究调试器。我读到调试器有软件断点(apparently 这些是最常用的断点)。这些通过将操作码的第一个字节替换为 Int 3(操作码 0xcc)来工作。 我已经读过程序的文本(/code)
我正在尝试尽可能多地摆脱 Delphi IDE 附带的编辑器。现在我正在寻找一种将 Delphi 的调试器插入不同编辑器的方法。 是否有隐藏的 API、命令行界面或类似的东西使其他应用程序能够设置调试
我是一名优秀的程序员,十分优秀!