Opening objects with a renamed namespace/assembly

2019-07-20 16:06发布

问题:

I have a set of objects in db4o format in a .dat file. The objects in that file are OldNamespace.MyObject, OldAssemblyName.

The problem is I've since renamed the namespace and assembly to something more permanent. Short of renaming the assembly and namespace (which is what I'm doing), is there a way of opening the objects into the new assembly/namespace names?

Or am I stuck forever with "MyTest3" for the assembly name and namespace?!

回答1:

I found the answer faster than I thought I would, in the wiki documentation.

This is the temporary way of doing it:

TypeAlias alias = new TypeAlias("OldNameSpace.OldTypeName, OldAssembly", "NewNameSpace.NewTypeName, NewAssembly");
Db4oFactory.Configure().AddAlias(alias);
IObjectContainer db = Db4oFactory.OpenFile(dbfilename);

The more permanent way (it's messy code but it's for a temp fix):

using (IObjectContainer db = Db4oFactory.OpenFile(dbfilename))
{
    var n = db.Ext().StoredClasses();
    foreach (var x in n)
    {
        System.Diagnostics.Debug.WriteLine(x.GetName());
    }
    var c1 = db.Ext().StoredClass("OldNameSpace.OldType, OldAssembly");//
    if (c1 != null)
        c1.Rename("NewNameSpace.OldType, NewAssembly");

    var c2 = db.Ext().StoredClass("System.Collections.Generic.List`1[[OldNameSpace.OldType, OldAssembly]], mscorlib");
    if (c2 != null)
        c2.Rename("System.Collections.Generic.List`1[[NewNameSpace.OldType, NewAssembly]], mscorlib");
}

As you can see 'ILists' need updating. Make sure when you save you do it to a new file, otherwise you will get both types in the output file.



标签: c# db4o