在Remoting / Serialization中发现了一个错误 [英] Found a bug in Remoting / Serialization

查看:70
本文介绍了在Remoting / Serialization中发现了一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个有趣的错误,我以为我会扔掉那里。它是当你正在远程调用与被调用的

类相同类型的参数时发生
,例如复制例程的第一行

成功,第二次失败:


类MyType():MarshalByRefObject

{

私人字符串名称;


public MyType()

{

}


public Copy(MyType myType )

{

//成功

this.name = myType.Name;


/ /如果myType是远程代理,则抛出异常

this.name = myType.name;

}


公共字符串名称

{

get

{

返回姓名;

}

}

}


-

Howard Swope [howardsnewsATspitzincDOTcom]

软件工程师

Spitz,Inc [ http://www.spitzinc.com ]

解决方案

并不是该方法具有''MyType''参数:


您无法访问远程对象的字段。你必须在Name属性中添加一个

访问器,并使用属性,而不是字段,在

复制方法中。


Remoting,IMO,必须非常小心地使用。可能会发生很多陷阱




以下为我工作,你会抛出什么异常?


// rdomas.cs

使用System;

使用System.Reflection;

命名空间Willys {

公共类HelloWorldWorker:MarshalByRefObject

{

string s;

public string S

{

get {return s;}

}

public void SayHello(HelloWorldWorker w)

{

this.s =" Hello";

this.s = wS;

this.s = ws +"世界;

}

}

}


// main.cs

....

public static void Main()

{

AppDomain newDomain = AppDomain.CreateDomain(" Second)应用程序域");

HelloWorldWorker远程=

(HelloWorldWorker)newDomain.CreateInstanceAndUnwra p(" rdomas" ;,

" Willys.HelloWorldWorker" );

remote.SayHello(远程);

Console.WriteLine(remote.S);

}

Willy。


Howard Swope < howardsnewsATspitzincDOTcom>在消息中写道

新闻:uU ************* @ TK2MSFTNGP12.phx.gbl ...

我遇到了一个有趣的bug我以为我会扔掉那里。当您正在远程调用与被调用的类型相同的参数时会发生这种情况,例如,复制例程的第一行在第二行失败时成功:

类MyType():MarshalByRefObject
{
私人字符串名称;

公共MyType()
{
}
公共副本(MyType myType)
//成功
this.name = myType.Name;

//如果myType抛出异常是一个远程代理
this.name = myType.name;
}

公共字符串名称
{
获取
{
返回名称;
}
}


Howard Swope [howardsnewsATspitzincDOTcom]
软件工程师
Spitz, Inc [ http://www.spitzinc.com]


我没看到文档,但我会接受你的意见。我想知道为什么会这样。显然数据存在,因为我可以通过该属性访问它。
。我想这是代理包装的方式

这会让事情变得困难,还是有其他原因让b
直接访问字段是不明智的?


< jn ********* @ gmail.com>在消息中写道

news:11 ********************** @ g44g2000cwa.googlegr oups.com ...

并不是该方法具有MyType参数:

您无法访问远程对象的字段。你必须在Name属性中添加一个set
访问器,并在Copy方法中使用属性而不是字段。

Remoting,IMO,必须非常小心地使用。可能会发生很多问题。



I came across an interesting bug I thought I would throw out there. It
occurs when you are remoting a parameter that is of the same type of the
class that is being called for example the first line of the Copy routine
succeeds where the second fails:

class MyType() : MarshalByRefObject
{
private string name;

public MyType()
{
}

public Copy(MyType myType)
{
// succeeds
this.name = myType.Name;

// throws exception if the myType is a remoting proxy
this.name = myType.name;
}

public string Name
{
get
{
return name;
}
}
}

--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]

解决方案

It''s not that the method has a ''MyType'' parameter:

You cannot access fields of a remoted object. You have to add a set
accessor to the Name property and use the property, not the field, in
the Copy method.

Remoting, IMO, must be used very carefully. There are a lot of gotchas
which can occur.


Following works for me, what exception is thrown on you?

// rdomas.cs
using System;
using System.Reflection;
namespace Willys {
public class HelloWorldWorker : MarshalByRefObject
{
string s;
public string S
{
get { return s;}
}
public void SayHello(HelloWorldWorker w)
{
this.s = "Hello";
this.s = w.S;
this.s = w.s + " world";
}
}
}

// main.cs
....
public static void Main()
{
AppDomain newDomain = AppDomain.CreateDomain("Second AppDomain");
HelloWorldWorker remote =
(HelloWorldWorker)newDomain.CreateInstanceAndUnwra p("rdomas",
"Willys.HelloWorldWorker");
remote.SayHello(remote);
Console.WriteLine(remote.S);
}

Willy.

"Howard Swope" <howardsnewsATspitzincDOTcom> wrote in message
news:uU*************@TK2MSFTNGP12.phx.gbl...

I came across an interesting bug I thought I would throw out there. It
occurs when you are remoting a parameter that is of the same type of the
class that is being called for example the first line of the Copy routine
succeeds where the second fails:

class MyType() : MarshalByRefObject
{
private string name;

public MyType()
{
}

public Copy(MyType myType)
{
// succeeds
this.name = myType.Name;

// throws exception if the myType is a remoting proxy
this.name = myType.name;
}

public string Name
{
get
{
return name;
}
}
}

--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]



I don''t see that in the documentation, but I''ll take your word for it. I
wonder why this is so. Obviously the data is there because I can access it
through the property. I guess it is the way that the proxy is wrapping
things up that makes this difficult or is there some other reason that
having direct access to the fields is unwise?

<jn*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

It''s not that the method has a ''MyType'' parameter:

You cannot access fields of a remoted object. You have to add a set
accessor to the Name property and use the property, not the field, in
the Copy method.

Remoting, IMO, must be used very carefully. There are a lot of gotchas
which can occur.



这篇关于在Remoting / Serialization中发现了一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆