In a custom layer I have a workspace variable (File GDB or SDE) that I need to persist (save/load).

Therefore I need to persist the ConnectionProperties of the workspace. Thankfully ConnectionProperties returns a PropertySet which implements IPersistStream so persisting this is trivial. But I also need to persist the type of workspace factory to use.

Unfortunately WorkspaceFactory does not implement IPersistStream so persisting it directly does not work (returns null when reading).

Is there a way to do this?

  • 1
    I think it's pretty safe to use the string comparison in your first example. Since they are esri constants they should never change. Aug 20 '15 at 23:11

The asker found a way to do this:

Persist the WorkspaceFactory.WorkspaceType of the workspace. Then I get the workspace factory with this method:

public static IWorkspaceFactory workspaceFactory(esriWorkspaceType type)
    string stype;
    if (type == esriWorkspaceType.esriLocalDatabaseWorkspace)
        stype = "esriDataSourcesGDB.FileGDBWorkspaceFactory";
    else if (type == esriWorkspaceType.esriRemoteDatabaseWorkspace)
         stype = "esriDataSourcesGDB.SdeWorkspaceFactory";
         return null;

    Type factoryType = Type.GetTypeFromProgID(stype);
    IWorkspaceFactory wsf = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
    return wsf;

I don't think this is good coding practice though using hardcoded strings. I would not have known these strings if I had not found them in code samples from ESRI.

Is there a better way to do this?

I tried using the Type:

UID uid = workspace.WorkspaceFactory.GetClassID();
Guid guid = new Guid(uid.Value as string);
Type workspaceFactoryType = Type.GetTypeFromCLSID(guid); 

This seemed more appealing since it would avoid hardcoded strings:

IWorkspaceFactory wf = Activator.CreateInstance(workspaceFactoryType) as IWorkspaceFactory;

The only problem is that I could not persist Type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.