输出参数,VB和C# [英] Out parameters, VB and C#

查看:71
本文介绍了输出参数,VB和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在从语言无关的界面生成VB和C#代码

定义,这就是为什么我要提高这个问题。 (这个问题似乎有点深奥,但在我的情况下这是真实的,而且仅仅是为了便利而不仅仅是b $ b。)


C#有一个out参数的概念(与传递值不同,并且通过引用传递
),但VB只能理解

传递值并传递引用。这意味着

的参数在参数中最终都是VB中的ByRef参数。

我想找到一种让VB源代码生成参数的方法

看起来像C#的参数。


这是一个非常简单的例子。假设我在VB中有以下内容:


''VB:

Public Sub setToFortyTwo(ByRef i As Integer)

i = 42

End Sub


我把这个方法捆绑到一个我用C#调用的程序集中。

目前,我可以将方法称为:


// C#:

int i = 0; //必须初始化,因为通过引用来调用

setToFortyTwo(ref i);


我想做的是以下内容:


// C#:

int i; //无需初始化参数

setToFortyTwo(out i);


是否有一些属性可用于指示VB标记

参数作为生成的汇编中的out参数?


我查看了语言参考,但我找不到

任何适用的东西。但是,可能有一些技巧可以使用

MarshalAs?基本上我需要的是一种方法来告诉VB编译器这是*真的*一个out参数和

我希望你在标记中将它标记为汇编。


我甚至会考虑对VB生成的代码进行后期处理

来更改参数类型,假设有一些文档

左右解释了元数据如何存储在程序集中或

一个目标文件。有人能指出我正确的方向,

没有更简单的解决方案吗?


谢谢,


Michi。

Hi,

I''m generating both VB and C# code from language-independent interface
definitions, which is why I''m raising this issue. (The problem apppears
to be somewhat esoteric, but it is real in my situation and is about
more than just convenience.)

C# has the notion of an out parameter (which is different
from pass by value and pass by reference), but VB only understands
pass by value and pass by reference. This means that parameters that
notionally are out parameters end up as ByRef parameters in VB.
I would like to find a way to have VB source code produce parameters
that look like out parameters to C#.

Here is a very simple example. Suppose I have the following in VB:

'' VB:
Public Sub setToFortyTwo(ByRef i As Integer)
i = 42
End Sub

I bundle this method into an assembly that I call from C#.
As it stands, I can call the method as:

// C#:
int i = 0; // Must initialize, because call is by reference
setToFortyTwo(ref i);

What I would like to do instead is the following:

// C#:
int i; // No need to initialize for out parameters
setToFortyTwo(out i);

Is there some attribute I can use to instruct VB to mark the
parameter as an out parameter in the generated Assembly?

I had a look through the language reference, but I can''t find
anything applicable. But, possibly, there is some trickery with
MarshalAs that could be used? Basically what I need is a way to
tell the VB compiler that "this is *really* an out parameter and
I want you to mark it as such in the assembly."

I''d even consider post-processing the code that is generated by VB
to change the parameter type, assuming that there is some documentation
around that explains how the metadata is stored in an assembly or
an object file. Can someone point me in the right direction for this,
failing an easier solution?

Thanks,

Michi.

推荐答案

Michi Henning< mi *** @ zeroc.com>写道:


< snip>
Michi Henning <mi***@zeroc.com> wrote:

<snip>
是否有一些属性我可以用来指示VB将
参数标记为在生成的汇编中输出参数?
Is there some attribute I can use to instruct VB to mark the
parameter as an out parameter in the generated Assembly?




我*认为你可以使用OutAttribute。


导入System.Runtime。 InteropServices


然后使用< Out>关于参数声明。


这似乎是通过快速测试来完成的,但你显然想要更仔细地看看它的价值。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



I *think* that you can use OutAttribute.

Import System.Runtime.InteropServices

and then use <Out> on the parameter declaration.

It seems to work from a quick test, but you''ll obviously want to look
at it a bit more carefully.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jon Skeet [C#MVP]写道:
Jon Skeet [C# MVP] wrote:
Michi Henning< mi *** @ zeroc .COM>写道:

< snip>
Michi Henning <mi***@zeroc.com> wrote:

<snip>
是否有一些属性我可以用来指示VB将
参数标记为out参数生成Assembly?
Is there some attribute I can use to instruct VB to mark the
parameter as an out parameter in the generated Assembly?



我*认为你可以使用OutAttribute。

导入System.Runtime.InteropServices

然后使用<缺货>关于参数声明。

它似乎可以通过快速测试来实现,但你显然想要更仔细地看一下它。


I *think* that you can use OutAttribute.

Import System.Runtime.InteropServices

and then use <Out> on the parameter declaration.

It seems to work from a quick test, but you''ll obviously want to look
at it a bit more carefully.




非常感谢!经过大量浏览文档后,我自己也发现了这个问题。像魅力一样:


''VB:

Public Sub setToFortyTwo(< System.Runtime.InteropServices.out()> ByRef i As Integer )

i = 42

End Sub


这很好用。


再次感谢您的帮助,我很抱歉大声呼救

只是为了那么一点: - |


干杯,

Michi。



Thanks muchly for that! After much browsing through the doc, I just found
this myself too. Works like a charm:

'' VB:
Public Sub setToFortyTwo(<System.Runtime.InteropServices.out( )> ByRef i As Integer)
i = 42
End Sub

This does the trick very nicely.

Thanks again for your help, and I apologize for having shouted for help
just that little bit too soon :-|

Cheers,

Michi.


Michi Henning< mi *** @ zeroc.com>写道:
Michi Henning <mi***@zeroc.com> wrote:
非常感谢!经过大量的文档浏览后,我自己也找到了这个。像魅力一样:

''VB:
Public Sub setToFortyTwo(< System.Runtime.InteropServices.out()> ByRef i as Integer)
i = 42
End Sub

这非常好用。


非常好。我个人会使用导入,只需将参数设为

(< Out> ByRef i as Integer)

但这是一个不同的故事:)

再次感谢您的帮助,我为那些太快的帮忙而道歉: - |
Thanks muchly for that! After much browsing through the doc, I just found
this myself too. Works like a charm:

'' VB:
Public Sub setToFortyTwo(<System.Runtime.InteropServices.out( )> ByRef i As Integer)
i = 42
End Sub

This does the trick very nicely.
Excellent. I''d personally use an import and just have the parameter as
(<Out> ByRef i as Integer)
but that''s a different story :)
Thanks again for your help, and I apologize for having shouted for help
just that little bit too soon :-|




没问题 - 从文档来看并不完全明显。它应该在ByVal vs ByRef文档中清楚地提到




-

Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ 〜双向飞碟

如果回复小组,请不要给我发邮件



No problem at all - it''s not entirely obvious from the docs. It should
really be mentioned clearly in the ByVal vs ByRef documentation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于输出参数,VB和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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