将包含Linq2Sql对象的对象序列化为StateServer

3

我目前正在尝试将一个对象序列化为一个托管在使用SessionState = Stateserver的服务器上的Web项目。

我已经将该对象标记为[Serializable],但该对象包含 LINQ2SQL DataContext 的一部分。我已经阅读过可以使用 DataContractSerializer 对其进行序列化,但正确执行序列化的位置在哪里呢?

我是否只需要实现 ISerializeable 接口,并在 GetObjectData() 函数内序列化我的 NodeObject 并将其添加到 SerializationInfo 中?有没有任何好的方法建议?

2个回答

0
现在完成了。我使用[Serializable]标记包含Linq对象的对象,并包括ISerializable接口。DBOItem是我的LinqObject。
    public void GetObjectData( SerializationInfo info, StreamingContext context )
    {
        PropertyInfo[] infos = this.DBOItem.GetType().GetProperties();
        foreach ( PropertyInfo pi in infos )
        {
            bool isAssociation = false;
            foreach ( object obj in pi.GetCustomAttributes( true ) )
            {
                if ( obj.GetType() == typeof( System.Data.Linq.Mapping.AssociationAttribute ) )
                { isAssociation = true; break; }
            }
            if ( !isAssociation )
            {
                if ( pi.GetValue( this.DBOItem, null ) != null )
                {
                    info.AddValue( pi.Name, pi.GetValue( this.DBOItem, null ) );
                }
            }
        }
    } 

需要包含用于反序列化的构造函数如下:

protected BusinessObjectBase( SerializationInfo info, StreamingContext context )
{
    this.DBOItem = new T();
            PropertyInfo[] properties = this.DBOItem.GetType().GetProperties();
            SerializationInfoEnumerator enumerator = info.GetEnumerator();

            while ( enumerator.MoveNext() )
            {
                SerializationEntry se = enumerator.Current;
                foreach ( PropertyInfo pi in properties )
                {
                    if ( pi.Name == se.Name )
                    {
                        pi.SetValue( this.DBOItem, info.GetValue( se.Name, pi.PropertyType ), null );
                    }
                }
            }
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接