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“.
it doesn’t work because doing:
or doing the same using
PropertyDescriptoralso 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