开发者

How do you save an object to a fooSettings.settings file in Visual Studio?

Can you save an object to a settings.settings file in Visual Studio? If so how? I think you can make your object serializable and it should persist, but I'm not sure if I'm doing it right i.e. when I try to get the object back it is always null.

Here is the code:

    [Serializable()]
    [XmlRoot(ElementName = "LayerTCA", IsNullable = false, Namespace = "http://somesite.com")]
    public class LayerTCA
    {
        //This is a COM object so I don't want to serialize this.
        IFeatureClass featureClass;

        string fullName;
        string basicName;

        public LayerTCA()
        {

        }

        public LayerTCA(IFeatureClass featureClass)
        {
            FeatureClass = featureClass;
        }


        public IFeatureClass FeatureClass
        {
            get { return featureClass; }
            set 
            {
                featureClass = value;
                fullName = featureClass.AliasName;
                basicName = StringHelper.StringAfterLastFullStop(fullName);
        开发者_StackOverflow中文版    }
        }

        [XmlAttribute(AttributeName = "BasicName")]  
        public string BasicName
        {
            get { return basicName; }
            set { basicName = value; }
        }


        [XmlAttribute(AttributeName = "FullName")]  
        public string FullName
        {
            get { return fullName; }
            set { fullName = value; }
        }

        public override string ToString()
        {
            return FullName;
        }

    }


I'd use a member variable that is an IFeatureClassName. The constructor would accept an IFeatureclass, then cast the featureclass to IDataset and set the member variable to IDataset.FullName. This will be an IName. All IName objects are persistable via IPersistStream. While not directly serializable to xml, an IPersistStream object can be written to a byte[] (via MemoryBlobStream). Can't remember if byte[] can be exposed via XmlAttribute, probably should use base64. Might also have an Open method would simply call IName.Open on the contained member featureclassname variable, returning an IFeatureclass.

Update: Here's some code that might help converting a featureclassname to and from a string.

public static void TestIt(IFeatureClass fc)
{
    string guidplusbase64Name = GetFullName((IDataset)fc);
    Debug.Print(guidplusbase64Name);
    IFeatureClass fc2 = OpenDataset(guidplusbase64Name) as IFeatureClass;
    Debug.Print(fc2.AliasName);            
}

public static string GetFullName(IDataset ds)
{
    IPersistStream ps = ds.FullName as IPersistStream;
    Guid g;
    ps.GetClassID(out g);            
    IMemoryBlobStream mbs = new MemoryBlobStreamClass();
    ps.Save(mbs,0);
    object bytes;
    ((IMemoryBlobStreamVariant)mbs).ExportToVariant(out bytes);            
    return String.Format("{0};{1}",g,Convert.ToBase64String((byte[])bytes));
}

public static IDataset OpenDataset(string guidplusbase64Name)
{
    int idx = guidplusbase64Name.IndexOf(";");
    string base64Name = guidplusbase64Name.Substring(idx+1);
    string guidString = guidplusbase64Name.Substring(0, idx);

    byte[] bytes = Convert.FromBase64String(base64Name);
    IMemoryBlobStream mbs = new MemoryBlobStreamClass();
    ((IMemoryBlobStreamVariant)mbs).ImportFromVariant(bytes);
    Type t = Type.GetTypeFromCLSID(new Guid(guidString));
    IName n = Activator.CreateInstance(t) as IName;
    ((IPersistStream)n).Load(mbs);
    IDataset ds = n.Open() as IDataset;
    return ds;
}

(updated to work with any IDataset, not just featureclasses) (update2: concatenating the clsid allows it to work with any dataset, previous version was hardwired for featureclassname)

Don't ask me why ESRI hasn't gotten around to implementing IName.NameString.

The NameString property is reserved for future use. When implemented, it will return a string representation of the locational component of the name object that may be persisted by applications.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜