Try this:
In the constructor of EmployeeVm:
public EmployeeVm(Employee employee)
{
this.InjectFrom<Employee>(employee);
stringBuilder = new StringBuilder();
stringBuilder.Append("<b>");
stringBuilder.Append(LastName.ToUpper());
stringBuilder.Append("</b>");
if (!string.IsNullOrEmpty(MiddleName))
{
stringBuilder.Append(", ");
stringBuilder.Append(MiddleName);
}
stringBuilder.Append(", ");
stringBuilder.Append(FirstName);
this.FullName = stringBuilder.ToString();
}
Convert FullName property to auto property:
public string FullName { get; set; }
Also, change the override method in the Employee to:
protected override bool Match(ConventionInfo c)
{
var isMatch = (c.SourceProp.Name == "PersonId" && c.TargetProp.Name == "EmployeeId") ||(c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Type == c.TargetProp.Type);
return isMatch;
}
You don’t need to override the match property or useSourceProp of LoopValueInjector. The match is for returning whether the source and target property matches or not and useSourceProp is for ignoring the SourceProperty so that it does not map to the target property.
In your scenario, the source property doesn’t have Full Name property and regarding match you don’t have to tell as if the name doesn’t match it won’t map the property.
The error was due to
LastName.ToUpper()
Which tried to convert the LastName property to upper before assigning the value. So, if you set the value in the constructor after valueinjector sets the value, the problem should be solved.