MapsterMapper / MapsterMapper/Mapster
[MapToTarget] [10.0.7] Mutable type not mutate when SourceType == DestinationType
A pull request for this has already been merged.
- #919 by @DocSvartz — merged
- Dominant language
- C#
- Stars
- 5.2k
- Forks
- 410
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
我有一个类如下:
`using CommunityToolkit.Mvvm.ComponentModel;`
using SignalSet.Utilities;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Text;
namespace SignalSet.Models;
```
public sealed class Station : ObservableValidator
{
public int RowNo
{
get;
set => SetProperty(ref field, value);
}
public string Location
{
get;
set => SetProperty(ref field, value);
} = string.Empty;
public string RealIp
{
get;
set => SetProperty(ref field, value);
} = string.Empty;
[RegularExpression(IpPattern, ErrorMessage = "不是一个有效的IP地址")]
public string Ip
{
get;
set => SetProperty(ref field, value, true);
} = string.Empty;
[DisplayName("端口"), Range(1000, 65535, ErrorMessage = "端口值必须设置为[1000-65535]之间的数字")]
public int Port
{
get;
set => SetProperty(ref field, value, true);
}
[RegularExpression(IpPattern, ErrorMessage = "不是一个有效的IP地址")]
public string Server
{
get;
set => SetProperty(ref field, value, true);
} = string.Empty;
[RegularExpression(IpPattern, ErrorMessage = "不是一个有效的IP地址")]
public string SubMask
{
get;
set => SetProperty(ref field, value, true);
} = string.Empty;
[RegularExpression(IpPattern, ErrorMessage = "不是一个有效的IP地址")]
public string GateWay
{
get;
set => SetProperty(ref field, value, true);
} = string.Empty;
public WorkMode Mode
{
get;
set => SetProperty(ref field, value);
}
public int SecurityKey
{
get;
set => SetProperty(ref field, value);
}
public string Address
{
get;
set => SetProperty(ref field, value);
} = string.Empty;
public string OldName
{
get;
set => SetProperty(ref field, value);
} = string.Empty;
public string DbAddress
{
get;
set => SetProperty(ref field, value);
} = string.Empty;
[RegularExpression(MacPattern, ErrorMessage = "不是一个有效的MAC地址")]
public string Mac
{
get;
set => SetProperty(ref field, value, true);
} = string.Empty;
public bool DoubleIp
{
get;
set => SetProperty(ref field, value);
}
public bool DoubleMac
{
get;
set => SetProperty(ref field, value);
}
[Browsable(false)]
public string Content
{
get;
set => SetProperty(ref field, value);
} = string.Empty;
public byte[] ToBytes(byte code = 0xAA)
{
var bytes = new byte[87];
MessageHead.CopyTo(bytes, 0);
MessageEnd.CopyTo(bytes, 83);
bytes[13] = code;
BitConverter.GetBytes((ushort)Port).Reverse().ToArray().CopyTo(bytes, 14);
IPAddress.Parse(Server).GetAddressBytes().CopyTo(bytes, 16);
IPAddress.Parse(RealIp).GetAddressBytes().CopyTo(bytes, 20);
IPAddress.Parse(Ip).GetAddressBytes().CopyTo(bytes, 24);
IPAddress.Parse(SubMask).GetAddressBytes().CopyTo(bytes, 28);
IPAddress.Parse(GateWay).GetAddressBytes().CopyTo(bytes, 32);
bytes[36] = (byte)Mode;
BitConverter.GetBytes(SecurityKey).Take(3).ToArray().CopyTo(bytes, 37);
Mac.Split(Colon).Select(p => Convert.ToByte(p, 16)).ToArray().CopyTo(bytes, 40);
var remark = Encoding.UTF8.GetBytes(Address ?? string.Empty);
remark = remark.Take(remark.Length > 32 ? 32 : remark.Length).ToArray();
bytes[46] = (byte)remark.Length;
remark.CopyTo(bytes, 47);
bytes[82] = (byte)(bytes.Take(82).Sum(p => p) & 0xFF);
return bytes;
}
public byte[] ToResetBytes() => ToBytes(0xBB);
public byte[] ToSetMacBytes() => ToBytes(0xAC);
}
```
我测试以下代码:
```
var station = new Station(){RowNo = 100;};
var station1 = new Station();
station.Adapt(station1);
```
它竟然没作用!!!以前的7.0.4版一点问题没有!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Station and Station1 reproduction using Adapt, then compare the behavior between Mapster 10.0.7 and 7.0.4. Done means adapting the mutable Station instance updates the destination as expected; linked pull request #919 indicates this work has already moved elsewhere.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100