Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are methods also serialized along with the data members in .NET?

The title is obvious, I need to know if methods are serialized along with object instances in C#, I know that they don't in Java but I'm a little new to C#. If they don't, do I have to put the original class with the byte stream(serialized object) in one package when sending it to another PC? Can the original class be like a DLL file?

like image 604
Lisa Avatar asked May 04 '10 18:05

Lisa


People also ask

Can serializable class have methods?

Serializable in Java If you want a class object to be serializable, all you need to do it implement the java. io. Serializable interface. Serializable in java is a marker interface and has no fields or methods to implement.

Which method is used to serialize?

The ObjectOutputStream class contains writeObject() method for serializing an Object. The ObjectInputStream class contains readObject() method for deserializing an object.

What does serialize mean with data?

Serialization is the process of converting a data object—a combination of code and data represented within a region of data storage—into a series of bytes that saves the state of the object in an easily transmittable form.

What is a serialization method?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.


2 Answers

No. The type information is serialized, along with state. In order to deserialize the data, your program will need to have access to the assemblies containing the types (including methods).

like image 73
Reed Copsey Avatar answered Oct 26 '22 04:10

Reed Copsey


It may be easier to understand if you've learned C. A class like

class C
{
    private int _m;
    private int _n;

    int Meth(int p)
    {
       return _m + _n + p;
    }
}

is essentially syntactic sugar for

typedef struct
{
   int _m;
   int _n;
   // NO function pointers necessary
} C;

void C_Meth(C* obj, int p)
{
   return obj->_m + obj->_n + p;
}

This is essentially how non-virtual methods are implemented in object-oriented languages. The important thing here is that methods are not part of the instance data.

like image 35
dan04 Avatar answered Oct 26 '22 03:10

dan04