Open codelovercc opened 1 year ago
I was getting the same error when trying to use the code generator for the same type of thing when using a Geometery
type from a 3rd party library. I was able to get it to work with this:
TypeAdapterConfig<Geometry, Geometry>.NewConfig()
.ConstructUsing(x => Geometry.DefaultFactory.CreateGeometry(x));
config.GenerateMapper("[name]Mapper")
.ForType<Agency>() ;
Hello, It Working with config
[TestMethod]
public void MappingInsiderCultureInfo()
{
TypeAdapterConfig<string, CultureInfo>.NewConfig()
.MapWith(str=> new CultureInfo(str));
TypeAdapterConfig<Dto, Entity>.NewConfig()
.Map(entity => entity.CultureInfo, dto => dto.CultureName);
TypeAdapterConfig<Entity Dto>.NewConfig()
.Map(dto => dto.CultureName, entity => entity.CultureInfo.Name);
var dto = new Dto
{
Id = "edcd0ab3-a949-4127-83c0-8a33f4108f09",
Content = "this is a test content",
Note = "for test",
CultureName = "zh-CN"
};
var mLocal = dto.Adapt<Entity>();
mLocal.Id.ToString().ShouldBe("edcd0ab3-a949-4127-83c0-8a33f4108f09");
mLocal.CultureInfo.Name.ShouldBe("zh-CN");
mLocal.Content.ShouldBe("this is a test content");
mLocal.Note.ShouldBe("for test");
}
I have similar issue but with a property without setting.
var x = new RealTest
{
Word = "hello"
};
x.Adapt<Test>().Dump("Mapped!"); // No default constructor
sealed class RealTest {
public string Word {get;set;}
}
sealed record Test(string Word){
public bool CanCall => true;
}
Tried Ignore
with TypeAdapterConfig
but it doesn't work.
Hi,
I use Mapster with version 7.3.0 and .Net 7.0.1
I have two classes.
And with mapster config
Now execute this code
var mLocal = dto.Adapt(); throws a Mapster.CompileException: Error while compiling.
No default constructor for type 'CultureInfo', please use 'ConstructUsing' or 'MapWith'
I tried use MapWith and it worked.
This configuration didn't work.
dto => CultureInfo.GetCultureInfo(dto.CultureName)
should work to create an instance ofCultureInfo
with out a constructor.Is there something I missed or is this a bug?