序列化包含 StateServer 的 linq2sql 对象的对象

发布于 2024-12-08 17:45:42 字数 355 浏览 0 评论 0原文

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

我将该对象标记为[Serialized],但该对象包含一个属于 LINQ2SQL DataContext 一部分的对象。我已经读到可以使用 DataContractSerializer 序列化这个序列化,但是正确的位置在哪里?

我是否只需要实现 ISerializeable 并在 GetObjectData() 函数内序列化我的 NodeObject 并将其添加到 SerializationInfo 中?有人对一种好方法有什么想法吗?

I am currently trying to serialize an object for a webproject that is hosted on a Server that uses SessionState = Stateserver.

I marked the object as [Serializable] but the object contains an object that is part of a LINQ2SQL DataContext. I already read that it is possible to serialize this one with the DataContractSerializer but where is the correct place to do that ?

Do I just need to implement ISerializeable and serialize my NodeObject inside the GetObjectData() Function and add it to the SerializationInfo ? Any one any ideas about a good approach ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2024-12-15 17:45:42

请找到以下链接。了解 [Serialized] VS DataContractSerializer 之间的区别将很有帮助。请根据您的适合性实施,因为您的项目没有太大差异。

http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

Please find the below link. It will be helpful to understand the difference between [Serializable] VS DataContractSerializer. Please implement based on your suitability as there is not much difference in your projcet.

http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

冰之心 2024-12-15 17:45:42

现在就做吧。我使用 [Serialized] 标记包含 Linqobject 的对象,并包含 ISerialized 接口。 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 ) );
                }
            }
        }
    } 

需要包含用于反序列化的 Ctor 如下所示:

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 );
                    }
                }
            }
}

Done it now. I marked my Objects that contain the Linqobject with [Serializable] and included the ISerializable Interface. DBOItem is my 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 ) );
                }
            }
        }
    } 

The Ctor that needs to be included for deserialisation looks like this :

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 );
                    }
                }
            }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文