gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:08:39 24 4
gpt4 key购买 nike

您可以将对象保存到 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;
}

}

最佳答案

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

更新:这里有一些代码可能有助于将要素类名称与字符串相互转换。

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,而不仅仅是要素类)(更新 2:连接 clsid 允许它与任何数据集一起工作,以前的版本是硬连线的 featureclassname)

别问我为什么 ESRI 还没有抽出时间实现 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.

关于c# - 如何将对象保存到 Visual Studio 中的 fooSettings.settings 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3052619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com