Why can't we have a class labeled [Serializable] and at the same time inheriting from MarshalByRefObject?

I know this sounds silly, but I'm just curious. My lecturer asked this question and we were clueless .: D

+4
source share
2 answers

Your lecturer is incorrect.

foreach(var type in typeof(Uri).Assembly.GetTypes())
{
    if (type.IsAbstract) continue;
    if (!Attribute.IsDefined(type, typeof(SerializableAttribute))) continue;
    if (!typeof(MarshalByRefObject).IsAssignableFrom(type)) continue;
    Console.WriteLine(type.FullName);
}

shows (and note that I only look at one assembly here):

System.Media.SoundPlayer
System.Net.FileWebRequest
System.Net.FileWebResponse
System.Net.HttpWebRequest
System.Net.HttpWebResponse
System.Diagnostics.EventLogEntry

and of course http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx shows:

[SerializableAttribute]
public class HttpWebRequest : WebRequest, 
    ISerializable

Noting also:

[SerializableAttribute]
public abstract class WebRequest : MarshalByRefObject, 
    ISerializable

For mscorlib ( typeof(object)) we get:

System.IO.Stream+SyncStream
System.IO.DirectoryInfo
System.IO.FileInfo
System.IO.MemoryStream
System.IO.TextReader+SyncTextReader
System.IO.StreamReader
System.IO.TextWriter+SyncTextWriter
System.IO.StreamWriter
System.IO.StringReader
System.IO.StringWriter
System.IO.Stream+NullStream
System.IO.TextReader+NullTextReader
System.IO.TextWriter+NullTextWriter

Enough concrete counterexample, I suspect.

, Remoting, , , - /, . .

  • [Serializable]
  • ( )
  • ; , , .

, , :

, MarshalByRefObject , / - , [Serializable]. : [Serializable] " " (: MarshalByRefObject " " ). , proxy/stub. .

+6

MarshalByRefObject Serializable, .

. MarshalByRefObject , . MarshalByRefObject. , ( ) - . , , ref - , .

, , , , , , - , .

0

Source: https://habr.com/ru/post/1537666/


All Articles