Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An exception is thrown when mapping a private property of a generic class: Method not found #1708

Open
3 tasks done
gaodayihao opened this issue Feb 4, 2025 · 2 comments
Open
3 tasks done
Labels
bug Something isn't working

Comments

@gaodayihao
Copy link

gaodayihao commented Feb 4, 2025

Please do the checklist before filing an issue:

  • I have read the documentation, including the FAQ
  • I can reproduce the bug using the latest prerelease version
  • I have searched existing discussion and issue to avoid duplicates

Describe the bug

An exception is thrown when mapping a private property of a generic class: Method not found
Image

refer: https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/9.0/unsafeaccessor-generics

Declaration code

using Riok.Mapperly.Abstractions;

public class Entity<TKey>
{
    public TKey Id { get; protected set; }
}

public class Student : Entity<Guid>
{
    public Student()
    {
        Id = Guid.NewGuid();
    }

    public int EntityVersion { get; protected set; } = 1;
}

public class StudentEto
{
    public Guid Id { get; set; }

    public int EntityVersion { get; set; }
}

[Mapper(RequiredMappingStrategy = RequiredMappingStrategy.Both, IncludedMembers = MemberVisibility.All)]
public static partial class Mapper
{
    public static partial Student MapperToStudent(StudentEto source);
}

Actual relevant generated code

// <auto-generated />
#nullable enable
public static partial class Mapper
{
    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
    public static partial global::Student MapperToStudent(global::StudentEto source)
    {
        var target = new global::Student();
        target.SetEntityVersion(source.EntityVersion);
        target.SetId(source.Id);
        return target;
    }
}
    
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
static file class UnsafeAccessor
{
    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
    [global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "set_EntityVersion")]
    public static extern void SetEntityVersion(this global::Student target, int value);

    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
    [global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "set_Id")]
    public static extern void SetId(this global::Entity<global::System.Guid> target, global::System.Guid value);
}

Expected relevant generated code

// <auto-generated />
#nullable enable
public static partial class Mapper
{
    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
    public static partial global::Student MapperToStudent(global::StudentEto source)
    {
        var target = new global::Student();
        target.SetEntityVersion(source.EntityVersion);
        UnsafeAccessor<Guid>.SetId(target, source.Id);
        return target;
    }
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
static file class UnsafeAccessor
{
    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
    [global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "set_EntityVersion")]
    public static extern void SetEntityVersion(this global::Student target, int value);
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
static file class UnsafeAccessor<TKey>
{
    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
    [global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "set_Id")]
    public static extern void SetId(global::Entity<TKey> target, TKey value);
}

Environment:

  • Mapperly Version: 4.2.0-next.0
  • Nullable reference types: enabled
  • .NET Version: .NET 9.0.101
  • Target Framework: .net9.0
  • Compiler version: '4.12.0-3.24572.7 (dfa7fc6b)'. Language version: latest (13.0).
  • C# Language Version: 13.0
  • IDE: Visual Studio v17.12.3
  • OS: Windows 10
@gaodayihao gaodayihao added the bug Something isn't working label Feb 4, 2025
@latonz
Copy link
Contributor

latonz commented Feb 11, 2025

Does the actual generated code work with .NET 8? Could be related to this breaking change.

@gaodayihao
Copy link
Author

Does the actual generated code work with .NET 8? Could be related to this breaking change.

Not sure, just tested in .NET 9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants