ValueInjecter不同的类型/属性名称

时间:2012-05-11 21:05:26

标签: c# valueinjecter

可以从这样的东西注入:

public class DomainEntity
{
    public Resource DescriptionResource { get; set; }
}

public class Resource
{
    public List<ResourceLocalization> Localizations { get; set; }
}

public class ResourceLocalization
{
    public string Culture { get; set; }
    public string Value { get; set; }
}

这样的事情:

public class DomainEntityViewModel
{
    public string Description { get; set; }
}

(DomainEntity.DescriptionResource.Localizations.First()。Value =&gt; DomainEntityViewModel.Description)

使用ValueInjecter。

谢谢!

1 个答案:

答案 0 :(得分:2)

你可以使用这种注射:

    public class MyInj : ConventionInjection
    {
        protected override bool Match(ConventionInfo c)
        {
            return c.TargetProp.Name == c.SourceProp.Name 
            && c.TargetProp.Type == typeof (string) 
            && c.SourceProp.Type == typeof (List<ResourceLocalization>);
        }
        protected override object SetValue(ConventionInfo c)
        {
            return ((List<ResourceLocalization>) c.SourceProp.Value).First().Value;
        }
    }