InAttribute 类

指示应将数据从调用方封送到被调用方,而不返回到调用方。

**命名空间:**System.Runtime.InteropServices
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.Parameter, Inherited:=False)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class InAttribute
    Inherits Attribute
用法
Dim instance As InAttribute
[AttributeUsageAttribute(AttributeTargets.Parameter, Inherited=false)] 
[ComVisibleAttribute(true)] 
public sealed class InAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Parameter, Inherited=false)] 
[ComVisibleAttribute(true)] 
public ref class InAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Parameter, Inherited=false) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class InAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Parameter, Inherited=false) 
ComVisibleAttribute(true) 
public final class InAttribute extends Attribute

备注

可以将此属性应用到参数。

InAttribute 是可选项。只有 COM Interop 和平台调用才支持此属性。如果缺少显式设置,Interop 封送拆收器将根据参数类型来假定规则,不论参数是通过引用传递还是通过值传递,也不论类型是可直接复制到本机结构中,还是不能直接复制到本机结构中。例如,始终假定 StringBuilder 类为 In/Out,并假定通过值传递的字符串数组为 In。

不能将 InAttribute 应用于使用 C# 样式 out 关键字修改过的参数。为避免将 Visual Basic 中的 In 关键字与 InAttribute(minus 属性)混淆,请使用 <[In]> 形式,用括号将该属性括起来。

当应用于数组和已设置格式的、非直接复制到本机结构中的类型时,InAttributeOutAttribute 结合起来使用特别有用。仅当同时应用这两个属性时,调用方才能看到被调用方对这些类型所做的更改。由于这些类型要求在封送处理期间进行复制,因此可以使用 InAttributeOutAttribute 以减少不必要的副本。

有关 InAttribute 对封送处理行为的影响的更多信息,请参见 方向属性

示例

下面的示例演示如何将 InAttributeOutAttribute 应用于将数组作为参数传递的平台调用原型。方向性属性的组合允许调用方查看由被调用方所做的更改。

' Declare a class member for each structure element.
< StructLayout( LayoutKind.Sequential, CharSet:=CharSet.Auto )> _
Public Class OpenFileName

   Public structSize As Integer = 0
   Public filter As String = Nothing
   Public file As String = Nothing
   ' ...
   
End Class 'OpenFileName

Public Class LibWrap
   ' Declare managed prototype for the unmanaged function.
   Declare Auto Function GetOpenFileName Lib "Comdlg32.dll" ( _
      <[In], Out> ByVal ofn As OpenFileName ) As Boolean
End Class 'LibWrap
// Declare a class member for each structure element.
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]  
public class OpenFileName 
{
    public int       structSize = 0;
    public string    filter = null;
    public string    file = null;
    // ...
}

public class LibWrap
{
   // Declare a managed prototype for the unmanaged function.
   [ DllImport( "Comdlg32.dll", CharSet=CharSet.Auto )]
   public static extern bool GetOpenFileName([ In, Out ] OpenFileName ofn );  
}
// Declare a class member for each structure element.

[StructLayout(LayoutKind::Sequential,CharSet=CharSet::Auto)]
public ref class OpenFileName
{
public:
   int structSize;
   String^ filter;
   String^ file;
   // ...
};

public ref class LibWrap
{
public:

   // Declare a managed prototype for the unmanaged function.

   [DllImport("Comdlg32.dll",CharSet=CharSet::Auto)]
   static bool GetOpenFileName( [In,Out]OpenFileName^ ofn );
};
// Declare a class member for each structure element.
/** @attribute StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)
 */
public class OpenFileName
{
    public int structSize = 0;
    public String filter = null;
    public String file = null;
    // ...
} //OpenFileName

public class LibWrap
{
    // Declare a managed prototype for the unmanaged function.
    /** @attribute DllImport("Comdlg32.dll", CharSet = CharSet.Auto)
     */
    public static native boolean GetOpenFileName(
        /** @attribute In()
            @attribute Out()
         */
        OpenFileName ofn);
} //LibWrap

继承层次结构

System.Object
   System.Attribute
    System.Runtime.InteropServices.InAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

InAttribute 成员
System.Runtime.InteropServices 命名空间
OutAttribute
StringBuilder

其他资源

可直接复制到本机结构中的类型和非直接复制到本机结构中的类型