ApplicationSettingBase类未保存自定义类型 [英] ApplicationSettingBase class is not saving custom type
问题描述
你好, LastUsedEnrollingDevice"的条目元素.我不知道如何调试它.非常感谢您的帮助.
我已经根据-
谢谢您的时间.
问候,
古尔姆斯
Hello, [UserScopedSetting] 这篇关于ApplicationSettingBase类未保存自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
I've implemented the my setting class according to example given at-
http://msdn.microsoft.com/hi-in/library/system.configuration.applicationsettingsbase(en-us).aspx
It has property-
public Unit LastUsedEnrollingDevice
{
get
{
return (Unit)appSettings["LastUsedEnrollingDevice"];
}
set
{
appSettings["LastUsedEnrollingDevice"] = value;
}
}
"Unit" is a service object exposed from one of WCF service, getting serialized/deserialized in WCF communication without any problem. "Save" method of "ApplicationSettingBase" does not throw any exception but when I tried to read it back it return null. I checked in user.config file and it has blank "Value" entry for for "LastUsedEnrollingDevice" element. I'm not getting any idea on how to debug it. I'd appreciate any help on it.
Thank you for your time.
Regards,
Gurmit推荐答案
为了序列化值,必须定义一个类型转换器(默认情况下),该类型转换器可以在字符串表示形式之间进行转换.将由设置类使用.因此,您的Unit类必须具有类型转换器.然后,您可以使用默认值属性设置默认值.请注意,持久化的实际值只是字符串表示形式,而不是实际对象本身.
此处提供了有关保留自定义类型所需的内容的完整讨论:In order to serialize the values you must define a type converter (by default) that can convert to and from the string representation that will be used by the settings class. Therefore your Unit class must have a type converter. You can then set the default value by using the default value attribute. Note that the actual value that is persisted is just the string representation and not the actual object itself.
A full discussion of what it takes to persist a custom type is given here: http://msdn.microsoft.com/hi-in/library/8eyb2ct1(en-us).aspx. Note that because settings are persisted you normally only use value types. This eliminates some potential issues.
Michael Taylor - 9/11/09
http://p3net.mvps.org