OutAttribute OutAttribute OutAttribute OutAttribute Class


指示应将数据从被调用方封送回调用方。Indicates that data should be marshaled from callee back to caller.

public ref class OutAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter, Inherited=false)]
public sealed class OutAttribute : Attribute
type OutAttribute = class
    inherit Attribute
Public NotInheritable Class OutAttribute
Inherits Attribute


下面的示例演示如何应用InAttributeOutAttribute平台调用来调用作为参数传递一个数组的原型。The following example shows how to apply the InAttribute and OutAttribute to a platform invoke prototype that passes an array as a parameter. 方向特性的组合允许调用方能够看见被调用方所做的更改。The combination of directional attributes allows the caller to see the changes made by the callee.

using namespace System;
using namespace System::Runtime::InteropServices;

// Declare a class member for each structure element.

public ref class OpenFileName
   int structSize;
   String^ filter;
   String^ file;
   // ...

public ref class LibWrap

   // Declare a managed prototype for the unmanaged function.

   static bool GetOpenFileName( [In,Out]OpenFileName^ ofn );

void main() {}
using System.Runtime.InteropServices;
using System;

// Declare a class member for each structure element.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
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.Unicode)]
    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);

public class MainMethod
    static void Main()
    { }

Imports System.Runtime.InteropServices

' Declare a class member for each structure element.
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
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 Unicode Function GetOpenFileName Lib "Comdlg32.dll" ( _
      <[In](), Out()> ByVal ofn As OpenFileName) As Boolean
End Class 'LibWrap

Public Class App
    Public Shared Sub Main()

    End Sub 'Main
End Class 'App


可以将此特性应用于参数。You can apply this attribute to parameters.

OutAttribute是可选的。The OutAttribute is optional. 该属性支持 COM 互操作和平台调用仅。The attribute is supported for COM interop and platform invoke only. 在没有显式设置时,互操作封送处理程序假定规则基于参数类型、 是否通过引用或通过值传递的参数和类型是否可直接复制或非直接复制。In the absence of explicit settings, the interop marshaler assumes rules based on the parameter type, whether the parameter is passed by reference or by value, and whether the type is blittable or non-blittable. 例如,StringBuilder类始终假定为输入/输出和一个按值传递的字符串数组被假定为中。For example, the StringBuilder class is always assumed to be In/Out and an array of strings passed by value is assumed to be In.

仅限扩展的行为永远不会是默认值封送处理行为的参数。Out-only behavior is never a default marshaling behavior for parameters. 您可以将应用OutAttribute通过将更改为仅限扩展的行为,相当于使用的输入/输出行为的引用传递的值和引用类型到outC# 中的关键字。You can apply the OutAttribute to value and reference types passed by reference to change In/Out behavior to Out-only behavior, which is equivalent to using the out keyword in C#. 例如,为只扩展,则可以更改值,默认情况下,仅在参数作为封送由传递的数组。For example, arrays passed by value, marshaled as In-only parameters by default, can be changed to Out-only. 但是,行为不会始终提供预期的语义时的类型包括所有 blittable 元素或字段,因为互操作封送处理程序使用固定。However, the behavior does not always provide expected semantics when the types include all-blittable elements or fields because the interop marshaler uses pinning. 如果您不关心将数据传递到被调用方,对外封送处理可以提供更好的性能非 blittable 类型。If you do not care about passing data into the callee, Out-only marshaling can provide better performance for non-blittable types.

组合InAttributeOutAttribute是特别有用时应用于数组和格式化,非 blittable 类型。Combining the InAttribute and OutAttribute is particularly useful when applied to arrays and formatted, non-blittable types. 调用方才能看到所做的更改被调用方对这些类型仅在应用这两个属性时。Callers see the changes a callee makes to these types only when you apply both attributes. 由于这些类型需要在封送处理期间复制,可以使用InAttributeOutAttribute以减少不必要的副本。Since these types require copying during marshaling, you can use InAttribute and OutAttribute to reduce unnecessary copies.

有关详细信息的效果OutAttribute封送处理行为,请参阅方向属性For more information on the effect of OutAttribute on marshaling behavior, see Directional Attributes.


OutAttribute() OutAttribute() OutAttribute() OutAttribute()

初始化 OutAttribute 类的新实例。Initializes a new instance of the OutAttribute class.


Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)


_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)


TypeId TypeId TypeId TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)