zerodowned

Moderator
How would I go about deserializing a custom class that doesn't inherit Item, Mobile, etc?

(CustomClass)reader.ReadCustomClass()
obviously doesn't work because the generic reader doesn't have a definition for ReadCustomClass

and if it makes a difference, I'm trying to do it from a persistencesave

CustomClass
Code:
using System;
using Server;
using Server.Items;
using Server.Network;
using Server.Mobiles;

namespace Server
{   
    public class CustomClass
    {
        [Constructable]
        public CustomClass()
        {
        }
       
        public CustomClass(GenericReader reader)
        {
            Deserialize(reader);
        }

        public void Serialize( GenericWriter writer )
        {
            writer.Write(0);
        }

        public void Deserialize( GenericReader reader )
        {
            var version = reader.ReadInt();
        }

    }
}
 
If the type of the class that you are serializing is mutable, then you want to save the type name first.
Code:
writer.Write( custom.GetType().FullName );

custom.Serialize( writer );
Code:
string typeName = reader.ReadString( );

Type type = Type.GetType( typeName, false ) ?? ScriptCompiler.FindTypeByFullName( typeName );

CustomClass custom = (CustomClass)Activator.CreateInstance( type, reader );

If your type is not mutable, you can just do this;
Code:
custom.Serialize( writer );
Code:
CustomClass custom = new CustomClass( reader );

With Vita-Nex: Core;
Code:
writer.WriteType( custom, ( w, t ) => custom.Serialize( w ) );
Code:
CustomClass custom = reader.ReadTypeCreate<CustomClass>( reader ); // CustomClass( reader )
 
Back