I'm doing a wrapper for an API and I have my json models which I would map into my entity models with MapsterMapper.
I have tried to simply do jsonModel.Adapt<EntityModel>()
with EntityModel
looking like this:
public class EntityModel {
public int Prop { get; internal set; }
public int Prop2 { get; internal set; }
internal EntityModel() {
}
}
But it yells that it cannot compile and find the right contructor. I have tried to trick a few things in the configuration but no chance.
For now, I have just removed the internal constructor, but my goal is to prevent the users of my api wrapper to instantiate entity by themselves.
Any idea to solve or workaround my issue?
Thanks in advance
According to the MapsterMapper documentation you can do this by explicity passing the constructor via reflection.
So:
var constructorInfo = typeof(EntityModel)
.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, CallingConventions.Any, new Type[0], null);
TypeAdapterConfig<??, <EntityModel>.NewConfig().MapToConstructor(constructorInfo);