The difference between C # MarshalByRefObject and Serializable

Source: Internet
Author: User

The classes in both ways are generally used for remote transmissions.

MarshalByRefObject is passed by reference serializable is passed by value, and now it is analyzed under what is the reference pass and what is the value passed.

It is important to understand this understanding of remoting or webservice.

MarshalByRefObject (Reference) This machine or the server is actually the same instance, but after the server is created you use that object locally. For example, Class A inherits MarshalByRefObject, so a class is created by the server, and the client can use this instance.

Now let's assume a class has a method called A,function return value to a string type this method has a series of operations. When calling this method, the client only gets a value returned by the server, and that sequence of operations is done on the server, which is called the stale customer service.

Serializable (value type) This is different, assuming that we just the Class A Funciton method requires a Class B as a parameter, B is a serializable class, that is, the definition of the class added [Serializable ()], if not added then this method will be an error. Let's explain by a remoting example.

First write a class that inherits MarshalByRefObject

public class Helloserver:marshalbyrefobject
{
Public HelloServer ()
{Console.WriteLine ("HelloServer activated"); }


Public String hellousermethod (user user)
{
string title;
if (user. Male)
title = "Sir";
Else
title = "Lady";

Console.WriteLine ("Server hello.hellomethod: Hello, {0}{1}", user.) Name,title);

Return "Hello," + user. Name + title;
}

}

and write a serializable class.

[Serializable]
public class User
{
Public User (string Name,bool male)
{
THIS.name = name;
This.male = male;
}
String Name= "";
BOOL Male=true;
public string Name
{
Get{return name;}
Set{name = value;}
}
public bool Male
{
Get{return male;}
Set{male = value;}
}

}

Now we will use them on both the server and the client side.

The service side is as follows:

public class Server
{
public static int Main (string [] args)
{

TcpChannel chan1 = new TcpChannel (8085);
HttpChannel chan2 = new HttpChannel (8086);

ChannelServices.RegisterChannel (CHAN1);
ChannelServices.RegisterChannel (CHAN2);


RemotingConfiguration.RegisterWellKnownServiceType (typeof (HelloServer), "SayHello",   Wellknownobjectmode.singleton); To create an instance of a class

System.Console.WriteLine ("Press Enter key to exit");
System.Console.ReadLine ();
return 0;
}

The client is as follows:

public class Client
{
public static void Main (string[] args)
{
Using the HTTP channel to get the remote object
HttpChannel chan2 = new HttpChannel ();
ChannelServices.RegisterChannel (CHAN2);
HelloServer obj1 = (HelloServer) Activator.GetObject (
typeof (RemotingSamples.HelloServer),
"Http://localhost:8086/SayHello");//Create an instance of the class
if (obj1 = = null)
{
System.Console.WriteLine (
"Could not locate HTTP server");
}
Console.WriteLine (
"Client1 TCP hellousermethod {0}",
Obj1. Hellousermethod (New User ("Mr. Zhang", true)); To use a class as a parameter
(The user must be a serializable argument)}

}

}

The difference between C # MarshalByRefObject and Serializable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.