如何将对象保存到Visual Studio中的fooSettings.settings文件?

时间:2010-06-16 10:53:31

标签: c# serialization arcobjects settings.settings

您可以将对象保存到Visual Studio中的settings.settings文件吗?如果是这样的话?我认为你可以使你的对象可序列化,它应该持久化,但我不确定我是否正确,即当我试图让对象恢复它总是为空。

以下是代码:

    [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);
            }
        }

        [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;
        }

    }

1 个答案:

答案 0 :(得分:1)

我使用的成员变量是IFeatureClassName。构造函数将接受IFeatureclass,然后将featureclass转换为IDataset并将成员变量设置为IDataset.FullName。这将是一个IName。所有IName对象都可以通过IPersistStream持久化。虽然不能直接序列化为xml,但可以将IPersistStream对象写入byte [](通过MemoryBlobStream)。不记得byte []是否可以通过XmlAttribute公开,可能应该使用base64。可能还有一个Open方法只需在包含的成员featureclassname变量上调用IName.Open,返回一个IFeatureclass。

更新:这里有一些代码可能有助于将featureclassname转换为字符串和从字符串转换。

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;
}

(更新为使用任何IDataset,而不仅仅是功能类) (update2:连接clsid允许它与任何数据集一起使用,以前的版本为featureclassname硬连线)

不要问我为什么ESRI没有实施IName.NameString

  

NameString属性是保留的   供将来使用。实施时,它   将返回一个字符串表示   名称的位置组件   可以持久化的对象   应用