'ref'关键字和AppDomains [英] 'ref' keyword and AppDomains

查看:72
本文介绍了'ref'关键字和AppDomains的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始使用C#时,我不确定如何正确处理引用(是否按值传递引用等).我错误地认为,传递将由被调用方法修改的对象时,需要使用'ref'关键字.

When I started using C# I was unsure of how references were treated exactly (whether they were being passed by value etc.). I wrongly thought the 'ref' keyword was needed when passing objects that would be modified by the called method.

然后,在阅读了 this 这样的线程之后,我意识到仅当您需要更改实际的引用/指针本身时,才需要使用"ref".

Then after reading threads like this, I realized 'ref' was only needed when you need to change the actual reference / pointer itself.

但是今天,当我通过远程调用传递参数时遇到了一个问题,实际上需要使用ref来修改对象的内容.在没有引用的情况下传递时,该对象保持不变.有人告诉我添加ref关键字,但有一段时间,我争论说只有在更改指针本身时才有必要,而不是所指向的内容.

But today I have come across an issue when passing a parameter via a remoting call, where ref was actually needed to modify the content of the object. When passed without ref, the object came back unchanged. I was told to add the ref keyword but I argued for a while that it was only necessary when you change the pointer itself, not the content that is being pointed to.

我已经在网上搜索过,只能找到

I have searched the net and could only find a single page that discusses it briefly. Is this a known issue and is anyone able to point to some documentation about it? It seems to me that I will have to use ref now for any parameter that is being modified via a remoting call.

推荐答案

添加"ref"可能有帮助,也可能没有帮助.这完全取决于特定的编组器实现的智能性.例如,如果您调用Web服务,则没有任何引用"将对您有所帮助-函数的参数根本不会通过网络发送回去.从服务返回的唯一一件事是函数的返回值.在处理远程处理时,您必须(至少在某种程度上)了解事情的实际运行方式-需要对参数进行序列化,然后通过某种电线"将其发送给被调用方,另一方面,将其反序列化,这一事实可以正常工作由服务器执行,结果序列化并发送回给您.这些结果是否包括对您首先传递的参数的更改,更多地取决于特定的远程实现,然后取决于为修饰参数而添加的"ref" ...

Adding "ref" might, or might not help. It all depends on the smartness of the particular marshaller implementation. If you call, for example, a web service, no amount of "ref"s is going to help you -- the parameters of the function are simply not sent back over the wire. The only thing that comes back from the service is the function's return value. When dealing with remoting you have to understand (at least to some degree) the way things actually work -- the fact that parameters need to be serialized and sent to the callee over some sort of "wire", deserialized on the other end, work is performed by the server, and the results serialized and sent back to you. Whether these results include changes to the parameters you passed in the first place depends more on the specific remoting implementation, then on the "ref"s that you add to decorate your parameters...

这篇关于'ref'关键字和AppDomains的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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