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

Adapt/AdaptToType DOES NOT working with anonymous object that returned from method #697

Open
tungnt-hemera opened this issue Apr 23, 2024 · 0 comments

Comments

@tungnt-hemera
Copy link

using Mapster;
using MapsterMapper;

var foo = new Foo(1, 2);
var t = foo.Get();

// var bar = t.Adapt<Bar>(); // <== Failed: '<>f__AnonymousType0<int,int>' does not contain a definition for 'Adapt'
// var bar = t.BuildAdapter().AdaptToType<Bar>(); // <== Failed '<>f__AnonymousType0<int,int>' does not contain a definition for 'BuildAdapter'

var mapper = new Mapper(TypeAdapterConfig.GlobalSettings);
var bar = mapper.Map<Bar>(t); // <== OK

// var t = new { A = 1, B = 2 }; 
// var bar = t.Adapt<Bar>(); //  <== Failed: '<>f__AnonymousType0<int,int>' does not contain a definition for 'Adapt'
// var bar = t.BuildAdapter().AdaptToType<Bar>(); // <== OK

Console.WriteLine($"A = {bar.A}; B = {bar.B}");

public class Foo
{
    private int a;
    private int b;
    public Foo(int a, int b)
    {
        this.a = a;
        this.b = b;
    }
    public dynamic Get()
    {
        return new { A = a, B = b };
    }
}

public class Bar
{
    public int A { get; set; }
    public int B { get; set; }
}

But IMapper works. Why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant