在<输出()>属性。有什么用处呢是服务? [英] The <Out()> Attribute. What useful purpose does is serve?

查看:140
本文介绍了在<输出()>属性。有什么用处呢是服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了System.Runtime.InteropServices <输出()> 属性存在。 但究竟是什么呢?我会很高兴,如果你可以使用下面的示例作为基础,你的答案。

 共享减少添加(BYVAL x As中整数,BYVAL y为整数,<输出()> ByRef的结果作为整数)
  结果= X + Y
 结束小组
 

解决方案

该属性的目的是双重的:

  • 呼叫现场处理,是否强制变量初始化与否
  • 编组站

如果你要调用该方法从C#,或类似的语言具有类似的语义,这样的参数将是已知的编译器并不需要的初始值。

在换句话说,你可以这样做:

  INT一个;
CallSomeMethodWithOutParameter(出);
 

和编译器知道有没有必要,以确保 A 已经拥有呼叫前的值。

在另一方面,如果没有属性,下面将需要,再次在C#:<​​/ P>

  INT A = 0; //&LT;  - 在这里通知初始化
CallSomeMethodWithOutParameter(REF一); //&LT;  - 和REF这里
 

另一个目的是为将要整理成不同的调用上下文的方法调用,例如通过P /调用,到不同的应用程序域,或到Web服务,通​​知编组程序有问题的参数将包含一个值时则该方法返回,但也没有必要在调用时,它通过任何值插入方法

这可能会使时需要的参数和返回值进行打包并运送到远程位置的区别在哪里实际调用经历。

在换句话说,如果你要指定一个方法调用至P /调用使用的,没有编组将在现有的参数值的完成时该方法的称为的,但是当该方法返回的它的价值被提升回你的电话code。

请注意,此优化是到编组日常使用,还是不行,这些是实现细节。属性只是告诉哪些参数是常规的可以的做到这一点的,它不是将始终遵循的指令。

Under System.Runtime.InteropServices the <Out()> Attribute exists. But what is it for? I would be glad if you could use the following example as base for your answers.

 Shared Sub Add(ByVal x As Integer, ByVal y As Integer, <Out()> ByRef Result As Integer)
  Result = x + y
 End Sub

解决方案

The purpose of that attribute is twofold:

  • Call-site handling, whether to enforce variable initialization or not
  • Marshalling

If you were to call that method from C#, or a similar language with similar semantics, such a parameter would be known to the compiler to not need an initial value.

In other words, you can do this:

int a;
CallSomeMethodWithOutParameter(out a);

and the compiler knows that there is no need to ensure that a already has a value before making the call.

On the other hand, without the attribute, the following would be needed, again in C#:

int a = 0;                               // <-- notice initialization here
CallSomeMethodWithOutParameter(ref a);   // <-- and ref here

The other purpose is for method calls that will be marshalled into a different calling context, for instance through P/Invoke, to a different app-domain, or to a web service, to notify marshalling routines that the parameter in question will contain a value when the method returns, but there is no need to pass any value into the method when calling it.

This might make a difference when parameters and return values needs to be packaged up and transported to the remote location where the actual call goes through.

In other words, if you were to specify that on a method call used through P/Invoke, no marshalling will be done of the existing parameter value when the method is called, but when the method returns its value is lifted back into your calling code.

Note that this optimization is up to the marshalling routine to use, or not, these are implementation details. The attribute just tells the routine which parameters it can do that with, it is not an instruction that will always be followed.

这篇关于在&lt;输出()&GT;属性。有什么用处呢是服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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