好吧,算出來。最後它非常簡單。您需要覆蓋負責從文檔創建.Net類型的DefaultSerializationBinder
。由於我的json文檔中有舊的名稱空間,因此我需要攔截該類型的創建以返回正確的類型。我將一個簡單的實現放在一起,這將允許您在創建JSON序列化程序時配置「遷移」。
public class NamespaceMigrationSerializationBinder : DefaultSerializationBinder
{
private readonly INamespaceMigration[] _migrations;
public NamespaceMigrationSerializationBinder(params INamespaceMigration[] migrations)
{
_migrations = migrations;
}
public override Type BindToType(string assemblyName, string typeName)
{
var migration = _migrations.SingleOrDefault(p => p.FromAssembly == assemblyName && p.FromType == typeName);
if(migration != null)
{
return migration.ToType;
}
return base.BindToType(assemblyName, typeName);
}
}
界面在哪裏
public interface INamespaceMigration
{
string FromAssembly { get; }
string FromType { get; }
Type ToType { get; }
}
對於那些想知道如何掛鉤它,它是這樣的:documentStore.Conventions.CustomizeJsonSerializer = serializer => serializer.Binder = new NamespaceMigrationSerializationBinder(migrations); – NeilD 2013-12-16 11:17:49
這是RavenDB中名稱空間遷移的絕佳解決方案。非常感謝!!! – marisks 2014-08-18 21:06:19
我想你可能也想重寫BindToName,或者你的保存可能有一個$ type =「」。你可以這樣做:public override void BindToName(Type serializedType,out string assemblyName,out string typeName) { typeName = serializedType.FullName; assemblyName = serializedType.Assembly.FullName; – pomarc 2017-12-27 16:47:51