Skip to content

[MapToTarget] [10.0.7] Mutable type not mutate when SourceType == DestinationType #918

@Ultramund1

Description

@Ultramund1

我有一个类如下:
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版一点问题没有!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions