Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将automapper配置文件与System.Text.Json.JsonSerializer一起使用_C#_Asp.net Core_Automapper_System.text.json - Fatal编程技术网

C# 将automapper配置文件与System.Text.Json.JsonSerializer一起使用

C# 将automapper配置文件与System.Text.Json.JsonSerializer一起使用,c#,asp.net-core,automapper,system.text.json,C#,Asp.net Core,Automapper,System.text.json,我正在使用Automapper,现在从newtonsoft转到system.text.json 以前在我的Profile类中,我做了以下工作(使用newtonsoft序列化): CreateMap() ForMember(s=>s.Settings,s=>s.MapFrom(a=>JsonConvert.SerializeObject(a.Settings)); 现在我想使用system.text.json: CreateMap<Application, ApplicationDto&g

我正在使用Automapper,现在从newtonsoft转到system.text.json

以前在我的Profile类中,我做了以下工作(使用newtonsoft序列化):

CreateMap()
ForMember(s=>s.Settings,s=>s.MapFrom(a=>JsonConvert.SerializeObject(a.Settings));
现在我想使用system.text.json:

CreateMap<Application, ApplicationDto>()
    ForMember(s => s.Settings, s => s.MapFrom(a => JsonSerializer.Serialize(a.Settings)));
CreateMap()
ForMember(s=>s.Settings,s=>s.MapFrom(a=>JsonSerializer.Serialize(a.Settings));

显然JsonSerializer.Serialize将无法工作-我收到一个错误:“表达式树可能不包含使用可选参数的调用或调用”

不确定下一票。无论如何,我最终创建了一个新函数来完成实际的JsonSerialization.Serialize()工作。然后我可以在CreateMap函数中调用它,比如-->.ForMember(s=>s.Settings,s=>s.MapFrom(a=>SerialiseToString(a.Settings));希望这对某人有所帮助。把它作为答案贴出来,并把它标记为接受。这样会很有帮助。我认为将可选的第二个参数显式地传递给就足够了,即
a=>JsonSerializer.Serialize(a.Settings,default(jsonserializeproptions))
。看见
CreateMap<Application, ApplicationDto>()
    ForMember(s => s.Settings, s => s.MapFrom(a => JsonSerializer.Serialize(a.Settings)));