天天看点

asp.net 2.0 Profile 的一些注意事项(序列化)

近日开发培训系统时,大量地使用到了 asp.net 2.0  的 profile 这个好用东西

配置文件是这样写

asp.net 2.0 Profile 的一些注意事项(序列化)

         < profile defaultProvider = " MyProfileProvider "  automaticSaveEnabled = " false " >

asp.net 2.0 Profile 的一些注意事项(序列化)

             <!-- 定义配置(profile) -->

asp.net 2.0 Profile 的一些注意事项(序列化)

             < providers >

asp.net 2.0 Profile 的一些注意事项(序列化)

                 < add name = " MyProfileProvider "  type = " System.Web.Profile.SqlProfileProvider "  applicationName = " CX "  connectionStringName = " CX.Services.CnString " />

asp.net 2.0 Profile 的一些注意事项(序列化)

             </ providers >

asp.net 2.0 Profile 的一些注意事项(序列化)

             < properties >

asp.net 2.0 Profile 的一些注意事项(序列化)

                 < add name = " UserSelectorGlobalVar "  type = " Profiles.UserSelectorGlobalVar "   allowAnonymous = " false " ></ add >

asp.net 2.0 Profile 的一些注意事项(序列化)

                 < add name = " UserExamDataSource "  type = " Profiles.UserExamDataSource "  allowAnonymous = " false " ></ add >

asp.net 2.0 Profile 的一些注意事项(序列化)

                 < add name = " ExamSelectedTimu "  type = " Profiles.ExamSelectedTimu "  allowAnonymous = " false " ></ add >

asp.net 2.0 Profile 的一些注意事项(序列化)

             </ properties >

asp.net 2.0 Profile 的一些注意事项(序列化)

         </ profile >

其中有一个类的代码如下:

asp.net 2.0 Profile 的一些注意事项(序列化)

using  System;

asp.net 2.0 Profile 的一些注意事项(序列化)

using  System.Collections.Generic;

asp.net 2.0 Profile 的一些注意事项(序列化)

using  System.Text;

asp.net 2.0 Profile 的一些注意事项(序列化)
asp.net 2.0 Profile 的一些注意事项(序列化)

namespace  Profiles

asp.net 2.0 Profile 的一些注意事项(序列化)
asp.net 2.0 Profile 的一些注意事项(序列化)

... {

asp.net 2.0 Profile 的一些注意事项(序列化)
asp.net 2.0 Profile 的一些注意事项(序列化)

    public class UserSelectorGlobalVar

asp.net 2.0 Profile 的一些注意事项(序列化)
asp.net 2.0 Profile 的一些注意事项(序列化)

    ...{

asp.net 2.0 Profile 的一些注意事项(序列化)

        public UserSelectorGlobalVar()

asp.net 2.0 Profile 的一些注意事项(序列化)
asp.net 2.0 Profile 的一些注意事项(序列化)

        ...{

asp.net 2.0 Profile 的一些注意事项(序列化)

        }

asp.net 2.0 Profile 的一些注意事项(序列化)

        private static System.Collections.Generic.Dictionary<Guid, Models.UserInfo> _SelectedUsers 

asp.net 2.0 Profile 的一些注意事项(序列化)

            = new System.Collections.Generic.Dictionary<Guid, Models.UserInfo>();

asp.net 2.0 Profile 的一些注意事项(序列化)
asp.net 2.0 Profile 的一些注意事项(序列化)

        public Dictionary<Guid, Models.UserInfo> SelectedUsers

asp.net 2.0 Profile 的一些注意事项(序列化)
asp.net 2.0 Profile 的一些注意事项(序列化)

        ...{

asp.net 2.0 Profile 的一些注意事项(序列化)
asp.net 2.0 Profile 的一些注意事项(序列化)

            get ...{ return _SelectedUsers; }

asp.net 2.0 Profile 的一些注意事项(序列化)
asp.net 2.0 Profile 的一些注意事项(序列化)

            set ...{ _SelectedUsers = value; }

asp.net 2.0 Profile 的一些注意事项(序列化)

        }

asp.net 2.0 Profile 的一些注意事项(序列化)

    }

asp.net 2.0 Profile 的一些注意事项(序列化)

}

asp.net 2.0 Profile 的一些注意事项(序列化)

呵呵,但是当调用this.profile.Save () 的时候说这个类出错,我想是没有将这个类标志为可以序列化的,于是加上属性

[Serializable]

但是问题依旧.

后来想起以前做的一个实验中将一个IList<XXX> 序列化后存入一个文件,选用SOAP那种方式就出错,好像是不支持吧,改用Binary 那种方式就好了. 但是profile 的序列化方式在哪里设定呢

后来想一下因该是在Web.Config 里面改的,借助VS2005 的代码提示功能发现

  < add name = " ExamSelectedTimu "  type = " Profiles.ExamSelectedTimu "  allowAnonymous = " false " ></ add >

这一句有一个属性:serializeAs 字面意思就能猜到啦,于是改为这样:

<add name="ExamSelectedTimu" type="Profiles.ExamSelectedTimu" serializeAs="Binary" allowAnonymous="false"></add>

Debug.... OK