在应用程序设置中存储自定义类型的问题 [英] problems with storing a custom type in application settings

查看:47
本文介绍了在应用程序设置中存储自定义类型的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在VS2005和C#中使用了一段时间的应用程序设置,但

通常使用标准类型。我一直试图在应用程序设置中存储一个自定义的

容器/类/类型,我看到了不稳定的

结果。我知道一个已知的缺陷,用户类没有出现在

视觉设计器的Property / Settings页面上的类型列表中我想知道是否b $ b我遇到了一些其他特殊的问题,或者如果有正确使用它的技巧,那么就需要使用它。


我在Property / Settings页面上定义了我的设置选择浏览

然后输入我的班级名称myNamespace.InfoContainerList,这基本上是这样的:


public class InfoContainerList:List< InfoContainer>

{//空 - 只需要给列表命名。

}

公共类InfoContainer:ICloneable

{

private string configName;

private string configSetting;

private bool typeA;

private bool rememberPassword;

。 。 。

}


我首先让程序为这个设置生成一个值进入

user.config,然后我复制了将值设置为VS中的app.config,假设

它会将其传播到需要去的地方。当我重新打开

属性/设置页面时,它抱怨app.settings

中的值已更改为''''(空字符串)和我想把它转移到

设置文件中吗?事实上,Property / Settings页面上的值是一个空的
字符串。

对于我的第二次尝试,我进入了value字段,点击了省略号,

有一个值向导/构建器,让我在

左边添加InfoContainer实例,并为右边的每个字段赋值。我保存/关闭了物品

页面并重新打开...并得到了相同的投诉,价值字段又是空的。

/>
那么为自定义类型提供默认值的正确方法是什么呢?

它在需要的地方传播并且它粘住 ?

解决方案

嗨Michael,


至于.NET 2.0应用程序设置属性,如果你想要使用

自定义类/类型,你需要实现以下任意一种持久性

对于cstst类型的意思:


**为它提供TypeConverter(实现FromSTring和ToString)


**或者你可以使它成为XmlSerialiable


这里有一些web关于此问题的常见问题和讨论,您可以看看

以获得更多想法:


#Client设置常见问题
http://blogs.msdn.com/rprabhu/articles/433979.aspx


#如何使用ApplicationSettingsBase保存自定义集合?
http://www.thescripts.com/forum/thread477600.html

此致,


Steven Cheng


Microsoft MSDN在线支持主管


============= =====================================


获取通过电子邮件通知我的帖子?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。


注意:MSDN托管新闻组支持服务是针对非紧急问题

如果社区或微软支持人员在1个工作日内做出初步回复是可以接受的。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx


==================================== ==============

此帖子按原样提供。没有保证,也没有赋予任何权利。


--------------------


>来自:=?Utf-8?B?bWljaGFlbCBzb3JlbnM =?=< m _ ******** @ newsgroup.nospam>
主题:存储自定义的问题键入应用程序设置
日期:太阳,2007年12月30日14:27:00 -0800

我已经在VS2005和C#中使用了应用程序设置一段时间,但
通常与标准类型。我一直试图在应用程序设置中存储自定义的容器/类/类型,我看到了不稳定的结果。我知道一个已知的缺陷,其中用户类没有显示





>可视化设计器中属性/设置页面上的类型列表和



I


> am想知道我是否遇到其他一些特殊问题,或者是否有





>只是技巧正确使用它。

我在Property / Settings页面上定义了我的设置,选择浏览
然后输入我的类名myNamespace.InfoContainerList,基本上是
就是这样:

公共类InfoContainerList:List< InfoContainer>
{//空 - 只需要为列表命名。
}
公共类InfoContainer:ICloneable
{

private string configName;

private string configSetting;

private bool typ eA;

private bool rememberPassword;

。 。 。

我首先让程序为
user.config生成此设置的值,然后将该设置值复制到VS中的app.config中,



假设


>它会将它传播到需要去的地方。当我重新打开
属性/设置页面时,它抱怨
中的值

app.settings


>已更改为''''(空字符串),我是否想要将其转换为
设置文件。事实上,Property / Settings页面上的值是



empty


> string 。
对于我的第二次尝试,我进入了值字段,点击了省略号,





>有一个值向导/构建器,让我在
上添加InfoContainer实例





> left,并为右边的每个字段赋值。我保存/关闭



房产


>页面并重新打开...并获得同样的抱怨,价值领域又是空的。

那么为自定义类型提供默认值的正确方法是什么呢?它会在需要的地方传播并且它粘住 ?


我明白你在说什么,但我观察到的并不完全同意

。如果没有TypeConverter或XmlSerializable,我就可以使它工作:一旦我在app.config中使用值编译,

应用程序就可以了。只有当我在VS中重新打开Property / Setting

页面时,我才有一些片状页面。行为。评论?


让我回到我之前跳过的问题:

Wdoing TypeConverter或XmlSerializable,我的app.config和

user.config文件在程序执行期间工作正常*。也就是说,我的

简单的InfoListContainer类...


公共类InfoContainerList:List< InfoContainer>

{//为空 - 只需要给名单命名。

}

公共类InfoContainer:ICloneable

{

private string configName;

private string configSetting;

private bool typeA;

private bool rememberPassword;

。 。 。

}


因此在user.config中作为一个组织良好的XML结构持续存在:


< setting名称= QUOT; ConfigList" serializeAs =" Xml">

< value>

< ArrayOfInfoContainer xmlns:xsi =" http://www.w3.org/2001/XMLSchema -instance"

xmlns:xsd =" http://www.w3.org/2001/XMLSchema">

< InfoContainer>

< ConnectionString>用户ID = foo;数据源= asb< / ConnectionString>

< ConfigName>默认< / ConfigName>

< DbType> SqlServer< ; / DbType>

< RememberPassword> false< / RememberPassword>

< / InfoContainer>

< InfoContainer>

< ConnectionString>用户ID =; Dsn = informaticaSource< / ConnectionString>

< ConfigName> myodbc< / ConfigName>

< DbType> Odbc< / DbType>

< RememberPassword> false< / RememberPassword>

< / InfoContainer>

< InfoContainer>

< ConnectionString>用户ID = a;数据源= asb< / ConnectionString>

< ConfigName> new-ora< / ConfigName>

< DbType> Oracle< / DbType>

< RememberPassword> false< / RememberPassword> ;

< / InfoContainer>

< / ArrayOfInfoContainer>

< / value>

< / setting>


我唯一的问题是我似乎无法在Property Designer的Settings

页面中设置默认值。如果你说它为什么它在运行时工作

不应该?为什么我有设计时间问题?


顺便说一句,我仍然没有在你的

未注释的代码示例中使用TypeConverter。除了字符串

" MySettingType"之外我需要更改什么? ?如果这是唯一的事情,那肯定看起来像是一个非常好的b $ b klunky技术,必须为每个我想要的类重复相同的代码

才能坚持下去。


I have worked with application settings in VS2005 and C# for awhile, but
usually with standard types. I have been trying to store a custom
container/class/type in an application setting and I have seen erratic
results. I am aware of one known defect where user classes do not show up in
the list of types on the Property/Settings page in the visual designer and I
am wondering if I am encountering some other peculiar issue, or if there are
just tricks to using it properly.

I defined my setting on the Property/Settings page by selecting "browse"
then typing in my class name myNamespace.InfoContainerList, which is
basically just this:

public class InfoContainerList : List<InfoContainer>
{ // empty--just needed to give a name to the List.
}
public class InfoContainer : ICloneable
{
private string configName;
private string configSetting;
private bool typeA;
private bool rememberPassword;
. . .
}

I first let the program generate a value for this setting into the
user.config, then I copied that setting value into app.config in VS, assuming
it would propagate it to where it needed to go. When I re-opened the
Property/Settings page however, it complained that the value in app.settings
had changed to '''' (the empty string) and did I want to propage that to the
Settings file. The value on the Property/Settings page was, in fact, an empty
string.
For my second attempt, I went to the value field, clicked the ellipsis, and
got a value wizard/builder that let me add instances of InfoContainer on the
left, and give values to each field on the right. I saved/closed the Property
pages and re-opened... and got the same complaint, and the value field was
again empty.

So what is the proper way to provide a default value to a custom type so
that it propagates where it needs to and it "sticks" ?

解决方案

Hi Michael,

As for the .NET 2.0 application settings properties, if you want to use
custom class/type, you need to implement either of the following persistent
means for the c ustom type:

** provide a TypeConverter(implement FromSTring and ToString ) for it

** or you can make it XmlSerialiable

Here are some web FAQ and threads discussing on this, you may have a look
to get some further ideas:

#Client Settings FAQ
http://blogs.msdn.com/rprabhu/articles/433979.aspx

#How to save custom collections using ApplicationSettingsBase?
http://www.thescripts.com/forum/thread477600.html
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------

>From: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= <m_********@newsgroup.nospam>
Subject: problems with storing a custom type in application settings
Date: Sun, 30 Dec 2007 14:27:00 -0800

I have worked with application settings in VS2005 and C# for awhile, but
usually with standard types. I have been trying to store a custom
container/class/type in an application setting and I have seen erratic
results. I am aware of one known defect where user classes do not show up

in

>the list of types on the Property/Settings page in the visual designer and

I

>am wondering if I am encountering some other peculiar issue, or if there

are

>just tricks to using it properly.

I defined my setting on the Property/Settings page by selecting "browse"
then typing in my class name myNamespace.InfoContainerList, which is
basically just this:

public class InfoContainerList : List<InfoContainer>
{ // empty--just needed to give a name to the List.
}
public class InfoContainer : ICloneable
{
private string configName;
private string configSetting;
private bool typeA;
private bool rememberPassword;
. . .
}

I first let the program generate a value for this setting into the
user.config, then I copied that setting value into app.config in VS,

assuming

>it would propagate it to where it needed to go. When I re-opened the
Property/Settings page however, it complained that the value in

app.settings

>had changed to '''' (the empty string) and did I want to propage that to the
Settings file. The value on the Property/Settings page was, in fact, an

empty

>string.
For my second attempt, I went to the value field, clicked the ellipsis,

and

>got a value wizard/builder that let me add instances of InfoContainer on

the

>left, and give values to each field on the right. I saved/closed the

Property

>pages and re-opened... and got the same complaint, and the value field was
again empty.

So what is the proper way to provide a default value to a custom type so
that it propagates where it needs to and it "sticks" ?


I understand what you are saying, but what I observed does not quite agree
with that. Without doing either a TypeConverter or XmlSerializable, I was
able to make it work: Once I compile with a value in app.config, the
application works just fine. It was only when I reopened the Property/Setting
page in VS that I had some "flaky" behavior. Comments?


Let me back up to my earlier question that you skipped:
Wdoing either a TypeConverter or XmlSerializable, my app.config and
user.config files are working *fine* during program execution. That is, my
simple InfoListContainer class...

public class InfoContainerList : List<InfoContainer>
{ // empty--just needed to give a name to the List.
}
public class InfoContainer : ICloneable
{
private string configName;
private string configSetting;
private bool typeA;
private bool rememberPassword;
. . .
}

persists as a nicely organized XML structure in user.config thusly:

<setting name="ConfigList" serializeAs="Xml">
<value>
<ArrayOfInfoContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<InfoContainer>
<ConnectionString>User ID=foo;Data Source=asb</ConnectionString>
<ConfigName>Default</ConfigName>
<DbType>SqlServer</DbType>
<RememberPassword>false</RememberPassword>
</InfoContainer>
<InfoContainer>
<ConnectionString>User ID=;Dsn=informaticaSource</ConnectionString>
<ConfigName>myodbc</ConfigName>
<DbType>Odbc</DbType>
<RememberPassword>false</RememberPassword>
</InfoContainer>
<InfoContainer>
<ConnectionString>User ID=a;Data Source=asb</ConnectionString>
<ConfigName>new-ora</ConfigName>
<DbType>Oracle</DbType>
<RememberPassword>false</RememberPassword>
</InfoContainer>
</ArrayOfInfoContainer>
</value>
</setting>

My only issue is that I cannot seem to set a default value in the Settings
Page of the Property Designer. Why is it working at runtime if you say it
should not be? And why am I having design-time issues?

BTW, I still do not follow the use of the TypeConverter from your
uncommented code sample. What do I need to change except the string
"MySettingType" ? If that is the only thing, that sure seems like a very
klunky technique, to have to repeat the identical code for every class I want
to persist.


这篇关于在应用程序设置中存储自定义类型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆