Skip to content

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

Open

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版一点问题没有!

Activity

  1. DocSvartz commented on Apr 17, 2026

    @DocSvartz
    Contributor

    Hi @Ultramund1 Do you mean version 10.0.8-pre01 ?

  2. Ultramund1 commented on Apr 17, 2026

    @Ultramund1
    Author

    @DocSvartz No,My version is 10.0.7

  3. Ultramund1 commented on Apr 17, 2026

    @Ultramund1
    Author

    我测试过了,继承自ObservableValidator的类都不行!
    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;
    
    public string Ip
    {
        get;
        set => SetProperty(ref field, value);
    } = string.Empty;
    
    public int Port
    {
        get;
        set => SetProperty(ref field, value);
    }
    
    public string Server
    {
        get;
        set => SetProperty(ref field, value);
    } = string.Empty;
    
    public string SubMask
    {
        get;
        set => SetProperty(ref field, value);
    } = string.Empty;
    
    public string GateWay
    {
        get;
        set => SetProperty(ref field, value);
    } = string.Empty;
    
    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;
    
    public string Mac
    {
        get;
        set => SetProperty(ref field, value);
    } = 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;
    

    }

  4. Ultramund1 commented on Apr 17, 2026

    @Ultramund1
    Author
  5. DocSvartz commented on Apr 17, 2026

    @DocSvartz
    Contributor

    @Ultramund1 Yes, I found what it's related to.

    You can roll back to version 10.0.6 or add an explicit default config.

    
    TypeAdapterConfig<Station, Station>
        .NewConfig();
    

    The fix will be in the next preview version.

  6. changed the title [-]Adapt No effect[/-] [+][MapToTarget] [10.0.7] Mutable type not mutate when SourceType == DestinationType[/+] on Apr 17, 2026
  7. linked a pull request that will close this issuefix IsNotSelfCreation detect #919on Apr 20, 2026
  8. DocSvartz commented on Apr 28, 2026

    @DocSvartz
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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