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