我在将来自不同项目 api 的 json 映射到我的模型 Customer 时遇到问题 public Dictionary<string, List<CustomerAddress>> CustomerAddress{ get; set; }
我为我的 json 设置定义了一个 ISerializationBinder,如下所示
public class KnownTypesBinder : ISerializationBinder
{
public IList<Type> KnownTypes { get; set; }
public string TypeFormat { get; private set; }
public KnownTypesBinder(string typeFormat)
{
TypeFormat = typeFormat;
}
public void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
assemblyName = null;
typeName = serializedType.Name;
}
public Type BindToType(string assemblyName, string typeName)
{
resolvedTypeName = string.Format(TypeFormat, typeName.Split('.').Last());
return Type.GetType(resolvedTypeName, true);
} …Run Code Online (Sandbox Code Playgroud)