使用 ValueInjecter 从 ExpandoObject 注入
我正在使用 ValueInjecter 进行对象映射,并尝试从 ExpandoObject 注入。我找到了一个从动态注入的例子。
public class Ac
{
public string Aa { get; set; }
}
[Test]
public void Aa()
{
var o = new { Aa = "aa" };
dynamic d = o;
var a = new Ac{ Aa = "bb" };
a.InjectFrom((object)d);
Assert.AreEqual(o.Aa, a.Aa);
}
但我还没有成功地让它与 ExpandoObject 一起工作。我该怎么做?
I'm using ValueInjecter for object mapping and I'm trying to inject from an ExpandoObject. I found an example of injecting from a dynamic.
public class Ac
{
public string Aa { get; set; }
}
[Test]
public void Aa()
{
var o = new { Aa = "aa" };
dynamic d = o;
var a = new Ac{ Aa = "bb" };
a.InjectFrom((object)d);
Assert.AreEqual(o.Aa, a.Aa);
}
But I have not been successful in getting it to work with an ExpandoObject. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用了与 Omu 发布的读取来自 XML 的 ExpandoObject 相同的方法。由于所有属性都以“字符串”形式出现,因此我使用“Convert.ChangeType”方法稍微调整了@Omu的答案:
I have used the same approach as Omu posted reading from a ExpandoObject that comes from a XML. As all the properties comes in as ´string´, so I have tweaked @Omu's answer a little using the ´Convert.ChangeType´ method: