假设您的类型以文档示例中所示的方式Plugin
继承:DevExpress.Xpo.XPObject
public class Plugin : XPObject {
// XPObject has a built-in Integer key and you can't add a custom key
public Plugin(Session session) : base(session) { }
string fMyProperty;
public string MyProperty {
get { return fMyProperty; }
set { SetPropertyValue(nameof(fMyProperty), ref fMyProperty, value); }
}
}
并假设您的反序列化代码已经可以访问 some Session session
,然后定义以下内容CustomCreationConverter<Plugin>
:
public class PluginConverter : CustomCreationConverter<Plugin>
{
DevExpress.Xpo.Session Session { get; }
public PluginConverter(DevExpress.Xpo.Session session) => this.Session = session ?? throw new ArgumentNullException();
public override Plugin Create(Type objectType) => new Plugin(Session);
}
并反序列化如下:
Session session; // Your session
var jsonsettings = new JsonSerializerSettings
{
Converters = { new PluginConverter(session), /* Other converters as required */ },
// Other settings as required
};
var pl = JsonConvert.DeserializeObject<Plugin>(filetext, jsonsettings);
反序列化类型时,将调用Plugin
转换器的方法以使用提供的会话来制造。Create()
Plugin
如果您使用多个会话,则需要JsonSerializerSettings
为每个新会话使用一个新会话。
此模式可用于任何继承自带XPObject
有Session
对象的公共构造函数的类型。