无法让 ValueInjecter 映射 COM 对象

发布于 2024-10-25 10:17:06 字数 1068 浏览 1 评论 0原文

请参阅以下代码。使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相对绾红妆 2024-11-01 10:17:06

它不起作用,因为这样做:

fwRule.GetType().GetProperties().Count()// is 0 

或使用 PropertyDescriptor 执行相同操作也会返回零,就像该对象没有属性一样,

解决方案是编写一个注入,该注入将从获取位置获取类型属性:

public class Same<T> : ValueInjection
{
   protected override void Inject(object source, object target)
   {
       var props = typeof (T).GetInfos().ToArray();
       var tp = target.GetInfos().ToArray();
       for (var i = 0; i < props.Count(); i++)
       {
          var prop = props[i];
          for (var j = 0; j < tp.Count(); j++)
          {
            if(prop.Name == tp[j].Name && prop.PropertyType == tp[j].PropertyType)
            tp[j].SetValue(target,prop.GetValue(source, null),null);
          }
        }
      }
  }

和用法:

rule.InjectFrom<Same<INetFwRule>>(fwRule);

这与默认的 InjectFrom() 相同,但它从提供的 Type 读取目标属性

it doesn't work because doing:

fwRule.GetType().GetProperties().Count()// is 0 

or doing the same using PropertyDescriptor also return zero, it's like the object doesn't has properties

the solution is to write an injection that will take the type from where to get the properties:

public class Same<T> : ValueInjection
{
   protected override void Inject(object source, object target)
   {
       var props = typeof (T).GetInfos().ToArray();
       var tp = target.GetInfos().ToArray();
       for (var i = 0; i < props.Count(); i++)
       {
          var prop = props[i];
          for (var j = 0; j < tp.Count(); j++)
          {
            if(prop.Name == tp[j].Name && prop.PropertyType == tp[j].PropertyType)
            tp[j].SetValue(target,prop.GetValue(source, null),null);
          }
        }
      }
  }

and the usage:

rule.InjectFrom<Same<INetFwRule>>(fwRule);

this is the same as the default InjectFrom() but it reads the target properties from the supplied Type

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文