无法让 ValueInjecter 映射 COM 对象
请参阅以下代码。使用 AutoMapper 时测试通过,但使用 ValueInjecter 时测试失败:
using NetFwTypeLib;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
INetFwPolicy2 policy = (INetFwPolicy2)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
INetFwRules fwRules = policy.Rules;
Rule rule = new Rule();
foreach (INetFwRule fwRule in fwRules)
{
if (fwRule.Name == "HomeGroup Out")
{
//AutoMapper.Mapper.CreateMap<INetFwRule, Rule>();
//AutoMapper.Mapper.Map(fwRule, rule);
rule.InjectFrom(fwRule);
break;
}
}
Assert.IsTrue(rule.Name == "HomeGroup Out");
}
}
public class Rule
{
public string Name { get; set; }
}
有什么想法吗?谢谢。
编辑:
根据Omu的回答,问题似乎与COM对象有关,而不仅仅是FirewallAPI.dll类。因此,我将标题从“无法获取 ValueInjecter 来映射 FirewallAPI.dll 类”更改为“无法获取 ValueInjecter 来映射 COM 对象”。
See the following code. The test passed when using AutoMapper, but failed when using ValueInjecter:
using NetFwTypeLib;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
INetFwPolicy2 policy = (INetFwPolicy2)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
INetFwRules fwRules = policy.Rules;
Rule rule = new Rule();
foreach (INetFwRule fwRule in fwRules)
{
if (fwRule.Name == "HomeGroup Out")
{
//AutoMapper.Mapper.CreateMap<INetFwRule, Rule>();
//AutoMapper.Mapper.Map(fwRule, rule);
rule.InjectFrom(fwRule);
break;
}
}
Assert.IsTrue(rule.Name == "HomeGroup Out");
}
}
public class Rule
{
public string Name { get; set; }
}
Any ideas? Thanks.
Edit:
Based on Omu's answer, it seems the problem is related to COM objects, not only FirewallAPI.dll classes. So I changed title from "Can't get ValueInjecter to map FirewallAPI.dll classes" to "Can't get ValueInjecter to map COM objects".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用,因为这样做:
或使用
PropertyDescriptor
执行相同操作也会返回零,就像该对象没有属性一样,解决方案是编写一个注入,该注入将从获取位置获取类型属性:
和用法:
这与默认的 InjectFrom() 相同,但它从提供的 Type 读取目标属性
it doesn't work because doing:
or doing the same using
PropertyDescriptor
also return zero, it's like the object doesn't has propertiesthe solution is to write an injection that will take the type from where to get the properties:
and the usage:
this is the same as the default InjectFrom() but it reads the target properties from the supplied Type