You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've found a problem with mapping objects, that have nested object with properties named the same as in parent object.
For example, I have classes: Class1 and Class2 that have child objects under property Inner (Class1Inner and Class2Inner).
Class1:
public class Class1
{
public int Id { get; set; }
public string Name { get; set; }
public Class1Inner Inner { get; set; }
public class Class1Inner
{
public int Id { get; set; }
public string Name { get; set; }
}
public override string ToString()
{
return $"[Class1][[Id: {Id}][Name: {Name}][Inner: [Id: {Inner.Id}][Name: {Inner.Name}]]]";
}
}
Class2:
public class Class2
{
public int Id { get; set; }
public string Name { get; set; }
public Class2Inner Inner { get; set; }
public class Class2Inner
{
public int Id { get; set; }
public string Name { get; set; }
}
public override string ToString()
{
return $"[Class2][[Id: {Id}][Name: {Name}][Inner: [Id: {Inner.Id}][Name: {Inner.Name}]]]";
}
}
RegisterMapping();
var class1Element = new Class1()
{
Id = 1,
Name = "First",
Inner = new Class1.Class1Inner()
{
Id = 11,
Name = "First Inner"
}
};
Console.WriteLine($"Element before mapping: {class1Element}");
var class2Element = class1Element.Map<Class1, Class2>();
Console.WriteLine($"Element after mapping: {class2Element}");
The result is:
Element before mapping: [Class1][[Id: 1][Name: First][Inner: [Id: 11][Name: First Inner]]]
Element after mapping: [Class2][[Id: 0][Name: ][Inner: [Id: 11][Name: First Inner]]]
Properties in Class2 were not mapped, but in Class2Inner were mapped correctly.
But when I change mapping to (I just moved inner class mapping to the top of the mapping sequence):
Element before mapping: [Class1][[Id: 1][Name: First][Inner: [Id: 11][Name: First Inner]]]
Element after mapping: [Class2][[Id: 1][Name: First][Inner: [Id: 11][Name: First Inner]]]
I have found a similar problem with mapper but in a bit different context, I have the following class declaration:
public class MyEditModel
{
public int Id { get; set; }
public string Title { get; set; }
public string Notes { get; set; }
public DropDownList<int> Authorized1 { get; set; } = new DropDownList<int>();
public DropDownList<int> Authorized2 { get; set; } = new DropDownList<int>();
}
and here is the DropDownList class:
` public class DropDownList: DropDownListBase
where SelectionType : IComparable
{
SelectionType _selection;
public SelectionType Selection
{
get => _selection;
set
{
_selection = value;
if (Items.Any())
{
foreach (var item in Items)
{
if (item.Value == _selection.ToString())
item.Selected = true;
else
item.Selected = false;
}
}
}
}
public DropDownList()
{
Selection = default(SelectionType);
}
public DropDownList(List<SelectListItem> list):this()
{
Items = list;
}
public DropDownList(List<SelectListItem> list, SelectionType item):this(list)
{
Selection = item;
}
public static DropDownList<SelectionType> Create<T>(IEnumerable<T> list, Expression<Func<T, string>> textExpr, Expression<Func<T, SelectionType>> valueExpr, SelectionType selected) where T:class
{
var items = BuildSelectItemList(list, textExpr, valueExpr);
return new DropDownList<SelectionType>(items, selected);
}
public static DropDownList<SelectionType> Create<T>(IEnumerable<T> list, Expression<Func<T, string>> textExpr, Expression<Func<T, SelectionType>> valueExpr) where T:class
{
var items = BuildSelectItemList(list, textExpr, valueExpr);
return new DropDownList<SelectionType>(items);
}
}`
Hi
I've found a problem with mapping objects, that have nested object with properties named the same as in parent object.
For example, I have classes:
Class1
andClass2
that have child objects under propertyInner
(Class1Inner
andClass2Inner
).Class1:
Class2:
I have mapping:
And when I execute code below:
The result is:
Properties in Class2 were not mapped, but in Class2Inner were mapped correctly.
But when I change mapping to (I just moved inner class mapping to the top of the mapping sequence):
The result is:
So everything is perfectly fine.
Additional informations:
Expressmapper version:
1.9.1
(from NuGet).NET Framework version:
4.6.2
The text was updated successfully, but these errors were encountered: