[英]Custom Type within ArrayList inside of the user profile in C#
I'm getting the following error when trying to save a custom class within my ArrayList within my user profile: 尝试在用户配置文件中的ArrayList中保存自定义类时出现以下错误:
There was an error generating the XML document. 生成XML文档时出错。 ---> System.InvalidOperationException: The type Com.xxx.MyDataClass was not expected.
---> System.InvalidOperationException:不应使用Com.xxx.MyDataClass类型。 Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
使用XmlInclude或SoapInclude属性可以指定静态未知的类型。
My web.config has something like: 我的web.config类似于:
<profile enabled="true" automaticSaveEnabled="false" defaultProvider="xxxxxxProvider">
<properties>
<add name="MyData" type="System.Collections.ArrayList"/>
</properties>
</profile>
I am trying to store my class called (something like) MyDataClass in an ArrayList into MyData. 我试图将我的类(类似MyDataClass)存储在ArrayList中的MyData中。 When I save I get the error above.
保存时,出现上述错误。 The class MyDataClass has just two members for now, both set as 'string'.
MyDataClass类目前只有两个成员,都设置为“字符串”。
I assume I have to tell the class has to serialize itself but I'm not sure how. 我假设我必须告诉类必须序列化自己,但是我不确定如何进行序列化。
Here's the class: 这是课程:
namespace Com.xxxx.DataClasses
{
public class MyDataClass : ISerializable
{
public string elem1;
public string elem2;
public string Elem1
{
get { return elem1; }
set { elem1 = value; }
}
public string Elem2
{
get { return elem2; }
set { elem2 = value; }
}
....
UPDATE: FIXED 更新:已修复
I was able to fix this issue via the following: 我可以通过以下方式解决此问题:
First of all here is a good resource: http://msdn.microsoft.com/en-us/library/d8b58y5d.aspx 首先,这里是一个很好的资源: http : //msdn.microsoft.com/en-us/library/d8b58y5d.aspx
I changed the profile entries to be: 我将配置文件条目更改为:
<profile enabled="true" automaticSaveEnabled="false" defaultProvider="xxxxxxProvider">
<properties>
<add name="MyData" type="mypackage.MyDataClass" serializeAs="binary"/>
</properties>
</profile>
and then I created MyDataClass to contain a List<> of MyDataClassInfo objects similar to: 然后创建MyDataClass以包含类似于MyDataClassInfo对象的List <>:
namespace Com.xxxx.DataClasses
{
[Serializable]
public class MyDataClass
{
List<MyDataClassInfo> myDataClassInfo;
public MyDataClassInfo()
{
myDataClassInfo = new List<MyDataClassInfo>;
}
public List<MyDataClassInfo> MyDataClassInfo
{
get;
set;
}
}
[Serializable]
public class MyDataClassInfo
{
public string elem1;
public string elem2;
public string Elem1
{
get { return elem1; }
set { elem1 = value; }
}
public string Elem2
{
get { return elem2; }
set { elem2 = value; }
}
....
The key here was 1) using the serializeAt="binary" and 2) using a class which contained a list instead of a list within the profile. 这里的关键是1)使用serializeAt =“ binary”和2)使用包含列表而不是配置文件中列表的类。
You should never use the ArrayList
class. 您永远不要使用
ArrayList
类。 It is deprecated as of .NET 2.0. 从.NET 2.0开始不推荐使用。
Use List<MyDataClass>
instead. 使用
List<MyDataClass>
代替。 It may even solve this problem for you. 它甚至可以为您解决此问题。
Try decorating MyDataClass with SerializableAttribute. 尝试用SerializableAttribute装饰MyDataClass。
namespace Com.xxxx.DataClasses
{
[Serializable]
public class MyDataClass
{
public string elem1;
public string elem2;
public string Elem1
{
get { return elem1; }
set { elem1 = value; }
}
public string Elem2
{
get { return elem2; }
set { elem2 = value; }
}
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.